home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / gpp-1_42.lha / g++-1.42.0 / cplus-class.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  191KB  |  6,275 lines

  1. /* Functions related to building and playing with classes.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@mcc.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* High-level class interface. */
  23.  
  24. #include "config.h"
  25. #include "tree.h"
  26. #include "cplus-tree.h"
  27. #include "flags.h"
  28. #include "rtl.h"
  29. #include "assert.h"
  30. #include <stdio.h>
  31.  
  32. #include "obstack.h"
  33. #define obstack_chunk_alloc xmalloc
  34. #define obstack_chunk_free free
  35.  
  36. extern int xmalloc ();
  37. extern void free ();
  38.  
  39. #define NULL 0
  40. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  41. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  42.  
  43. /* See cplus-decl.c for comment of this variable.  */
  44. extern int flag_int_enum_equivalence;
  45.  
  46. /* some statistics gathering help.  */
  47. static int n_vtables, n_vtable_entries, n_vtable_searches, n_vtable_elems;
  48. static int n_convert_harshness, n_compute_conversion_costs, n_build_method_call;
  49. static int n_inner_fields_searched;
  50.  
  51. /* Compute the ease with which a conversion can be performed
  52.    between an expected and the given type.  */
  53. static int convert_harshness ();
  54.  
  55. /* in decl.c.  */
  56. extern tree lookup_tag_current_binding_level ();
  57.  
  58. /* in method.c.  */
  59. extern void do_inline_function_hair ();
  60.  
  61. /* Way of stacking class types.  */
  62. static tree *current_class_base, *current_class_stack;
  63. static int current_class_stacksize;
  64.  
  65. struct class_level
  66. {
  67.   /* The previous class level.  */
  68.   struct class_level *level_chain;
  69.  
  70.   /* The class instance variable, as a PARM_DECL.  */
  71.   tree decl;
  72.   /* The class instance variable, as an object.  */
  73.   tree object;
  74.   /* The virtual function table pointer
  75.      for the class instance variable.  */
  76.   tree vtable_decl;
  77.  
  78.   /* Name of the current class.  */
  79.   tree name;
  80.   /* Type of the current class.  */
  81.   tree type;
  82.  
  83.   /* Flags for this class level.  */
  84.   int this_is_variable;
  85.   int memoized_lookups;
  86.   int save_memoized;
  87.   int unused;
  88. };
  89.  
  90. tree current_class_decl, C_C_D;    /* PARM_DECL: the class instance variable */
  91. tree current_vtable_decl;
  92.  
  93. /* The following two can be derived from the previous one */
  94. tree current_class_name;    /* IDENTIFIER_NODE: name of current class */
  95. tree current_class_type;    /* _TYPE: the type of the current class */
  96. tree prev_class_type;        /* _TYPE: the previous type that was a class */
  97.  
  98. static tree get_vtable_name (), get_vfield_name ();
  99. tree the_null_vtable_entry;
  100.  
  101. /* Way of stacking langauge names.  */
  102. static tree *current_lang_base, *current_lang_stack;
  103. static int current_lang_stacksize;
  104.  
  105. /* Names of languages we recognize.  */
  106. tree lang_name_c, lang_name_cplusplus;
  107. tree current_lang_name;
  108.  
  109. tree minus_one_node;
  110.  
  111. /* When layout out an aggregate type, the size of the
  112.    basetypes (virtual and non-virtual) is passed to layout_record
  113.    via this node.  */
  114. static tree base_layout_decl;
  115.  
  116. #if 0
  117. /* Make sure that the tag NAME is defined *in the current binding level*
  118.    at least as a forward reference.
  119.    CODE says which kind of tag NAME ought to be.
  120.  
  121.    Not used for C++.  Not maintained.  */
  122.  
  123. tree
  124. start_struct (code, name)
  125.      enum tree_code code;
  126.      tree name;
  127. {
  128.   /* If there is already a tag defined at this binding level
  129.      (as a forward reference), just return it.  */
  130.   register tree ref = 0;
  131.  
  132.   if (name != 0)
  133.     ref = lookup_tag (code, name, current_binding_level, 1);
  134.   if (ref && TREE_CODE (ref) == code)
  135.     {
  136.       if (TYPE_FIELDS (ref))
  137.     error ((code == UNION_TYPE ? "redefinition of `union %s'"
  138.         : "redefinition of `struct %s'"),
  139.            IDENTIFIER_POINTER (name));
  140.  
  141.       return ref;
  142.     }
  143.  
  144.   /* Otherwise create a forward-reference just so the tag is in scope.  */
  145.  
  146.   ref = make_lang_type (code);
  147.   /* Must re-synch this with xref_tag if you are going to use it.  */
  148.   assert (0);
  149.   pushtag (name, ref);
  150.   return ref;
  151. }
  152. #endif
  153.  
  154. /* Virtual baseclass things.  */
  155. tree
  156. build_vbase_pointer (exp, type)
  157.      tree exp, type;
  158. {
  159.   char *name;
  160.  
  161.   name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1);
  162.   sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type));
  163.   return build_component_ref (exp, get_identifier (name), 0, 0);
  164. }
  165.  
  166. /* Build multi-level access to EXPR using hierarchy path PATH.
  167.    CODE is PLUS_EXPR if we are going with the grain,
  168.    and MINUS_EXPR if we are not (in which case, we cannot traverse
  169.    virtual baseclass links).
  170.  
  171.    TYPE is the type we want this path to have on exit.
  172.  
  173.    ALIAS_THIS is non-zero if EXPR in an expression involving `this'.  */
  174. tree
  175. build_vbase_path (code, type, expr, path, alias_this)
  176.      enum tree_code code;
  177.      tree type;
  178.      tree expr;
  179.      tree path;
  180.      int alias_this;
  181. {
  182.   register int changed = 0;
  183.   tree last = NULL_TREE, last_virtual = NULL_TREE;
  184.   int fixed_type_p = 0 && resolves_to_fixed_type_p (expr);
  185.   tree basetype;
  186.   tree offset = integer_zero_node;
  187.  
  188.   if (TREE_CHAIN (path))
  189.     path = nreverse (copy_list (path));
  190.  
  191.   basetype = TREE_VALUE (path);
  192.   while (path)
  193.     {
  194.       if (TREE_VIA_VIRTUAL (path))
  195.     {
  196.       last_virtual = TYPE_MAIN_VARIANT (TREE_VALUE (path));
  197.       if (code == PLUS_EXPR)
  198.         {
  199.           changed = ! fixed_type_p;
  200.  
  201.           if (last)
  202.         expr = convert_pointer_to (TREE_VALUE (last), expr);
  203.           if (changed)
  204.         expr = build_vbase_pointer (build_indirect_ref (expr, 0),
  205.                         last_virtual);
  206.           else
  207.         offset = ASSOC_OFFSET (value_member (last_virtual,
  208.                              CLASSTYPE_VBASECLASSES (basetype)));
  209.           /* Happens in the case of parse errors.  */
  210.           if (expr == error_mark_node)
  211.         return expr;
  212.         }
  213.       else
  214.         {
  215.           error_with_aggr_type (last_virtual, "cannot cast up from virtual baseclass `%s'");
  216.           return error_mark_node;
  217.         }
  218.     }
  219.       last = path;
  220.       path = TREE_CHAIN (path);
  221.     }
  222.   /* LAST is now the last basetype on the path.  */
  223.   last = TREE_VALUE (last);
  224.  
  225.   /* If we go through any virtual base pointers, make sure that
  226.      casts to BASETYPE from the last virtual base class use
  227.      the right value for BASETYPE.  */
  228.   if (changed)
  229.     {
  230.       tree intype = TREE_TYPE (TREE_TYPE (expr));
  231.       if (TYPE_MAIN_VARIANT (intype) == TYPE_MAIN_VARIANT (last))
  232.     basetype = intype;
  233.       else
  234.     {
  235.       basetype = get_base_type (last, TYPE_MAIN_VARIANT (intype), 0);
  236.       offset = CLASSTYPE_OFFSET (basetype);
  237.     }
  238.     }
  239.   else
  240.     {
  241.       if (last_virtual && last != last_virtual)
  242.     basetype = get_base_type (last, last_virtual, 0);
  243.       else
  244.     basetype = last;
  245.  
  246.       offset = genop (code, offset, CLASSTYPE_OFFSET (basetype));
  247.       /* 900324 JRV: If code was MINUS_EXPR, genop returned a negative
  248.      offset, so shouldn't the code always be PLUS_EXPR now? */
  249.       code = PLUS_EXPR;
  250.     }
  251.  
  252.   if (TREE_INT_CST_LOW (offset))
  253.     {
  254.       /* For multiple inheritance: if `this' can be set by
  255.      any function, then it could be 0 on entry
  256.      to any function.  Preserve such zeroness here.
  257.      Otherwise, only in the case of constructors need
  258.      we worry, and in those cases, it will be zero,
  259.      or initialized to some legal value to which we may
  260.      add.  */
  261.       tree addr = TREE_CODE (expr) == ADDR_EXPR
  262.     ? expr : save_expr (expr);
  263.       expr = build (code, type, addr, offset);
  264.  
  265.       if (alias_this == 0 || flag_this_is_variable)
  266.     return build (COND_EXPR, type,
  267.               build (EQ_EXPR, integer_type_node, addr, integer_zero_node),
  268.               build1 (NOP_EXPR, type, addr),
  269.               expr);
  270.     }
  271.   return build1 (NOP_EXPR, type, expr);
  272. }
  273.  
  274. /* Virtual function things.  */
  275.  
  276. /* Virtual functions to be dealt with after laying out our
  277.    virtual base classes (only if the type has any).  */
  278. static tree pending_hard_virtuals;
  279.  
  280. /* The names of the entries in the virtual table structure.  */
  281. static tree delta_name, pfn_name;
  282.  
  283. /* Temporary assoc list to memoize lookups of the left-most non-virtual
  284.    baseclass B in a lattice topped by T.  B can appear multiple times
  285.    in the lattice.
  286.    TREE_PURPOSE is B's TYPE_MAIN_VARIANT.
  287.    TREE_VALUE is the path by which B is reached from T.
  288.    TREE_TYPE is B's real type.
  289.  
  290.    If TREE_TYPE is NULL_TREE, it means that B was reached via
  291.    a virtual baseclass.
  292.    N.B.: This list consists of nodes on the temporary obstack.  */
  293. static tree leftmost_baseclasses;
  294.  
  295. /* Build an entry in the virtual function table.
  296.    DELTA is the offset for the `this' pointer.
  297.    PFN is an ADDR_EXPR containing a pointer to the virtual function.
  298.    Note that the index (DELTA2) in the virtual function table
  299.    is always 0.  */
  300. tree
  301. build_vtable_entry (delta, pfn)
  302.      tree delta, pfn;
  303. {
  304.   tree elems = tree_cons (NULL_TREE, delta,
  305.               tree_cons (NULL_TREE, integer_zero_node,
  306.                      build_tree_list (NULL_TREE, pfn)));
  307.   tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
  308.   TREE_LITERAL (entry) = 1;
  309.   TREE_STATIC (entry) = 1;
  310.   TREE_READONLY (entry) = 1;
  311.  
  312. #ifdef GATHER_STATISTICS
  313.   n_vtable_entries += 1;
  314. #endif
  315.  
  316.   return entry;
  317. }
  318.  
  319. /* Given an object INSTANCE, return an expression which yields
  320.    the virtual function corresponding to INDEX.  There are many special
  321.    cases for INSTANCE which we take care of here, mainly to avoid
  322.    creating extra tree nodes when we don't have to.  */
  323. tree
  324. build_vfn_ref (ptr_to_instptr, instance, index)
  325.      tree *ptr_to_instptr, instance;
  326.      tree index;
  327. {
  328.   extern int building_cleanup;
  329.   tree vtbl, aref;
  330.   tree basetype = TREE_TYPE (instance);
  331.  
  332.   if (TREE_CODE (basetype) == REFERENCE_TYPE)
  333.     basetype = TREE_TYPE (basetype);
  334.  
  335.   if (instance == C_C_D)
  336.     {
  337.       if (current_vtable_decl == NULL_TREE
  338.       || current_vtable_decl == error_mark_node
  339.       || get_base_type (DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type)), basetype, 0) == NULL_TREE)
  340.     vtbl = build_indirect_ref (build_vfield_ref (instance, basetype));
  341.       else
  342.     vtbl = current_vtable_decl;
  343.     }
  344.   else
  345.     {
  346.       if (optimize)
  347.     {
  348.       /* Try to figure out what a reference refers to, and
  349.          access its virtual function table directly.  */
  350.       tree ref = 0;
  351.  
  352.       if (TREE_CODE (instance) == INDIRECT_REF
  353.           && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
  354.         ref = TREE_OPERAND (instance, 0);
  355.       else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
  356.         ref = instance;
  357.  
  358.       if (ref && TREE_CODE (ref) == VAR_DECL
  359.           && DECL_INITIAL (ref))
  360.         {
  361.           tree init = DECL_INITIAL (ref);
  362.  
  363.           while (TREE_CODE (init) == NOP_EXPR
  364.              || TREE_CODE (init) == REFERENCE_EXPR)
  365.         init = TREE_OPERAND (init, 0);
  366.           if (TREE_CODE (init) == ADDR_EXPR)
  367.         {
  368.           init = TREE_OPERAND (init, 0);
  369.           if (IS_AGGR_TYPE (TREE_TYPE (init))
  370.               && (TREE_CODE (init) == PARM_DECL
  371.               || TREE_CODE (init) == VAR_DECL))
  372.             instance = init;
  373.         }
  374.         }
  375.     }
  376.  
  377.       if (IS_AGGR_TYPE (instance)
  378.       && (TREE_CODE (instance) == RESULT_DECL
  379.           || TREE_CODE (instance) == PARM_DECL
  380.           || TREE_CODE (instance) == VAR_DECL))
  381.     vtbl = CLASS_ASSOC_VTABLE (basetype);
  382.       else
  383.     vtbl = build_indirect_ref (build_vfield_ref (instance, basetype), 0);
  384.     }
  385.   aref = build_array_ref (vtbl, index);
  386.   if (!building_cleanup && TREE_CODE (aref) == INDIRECT_REF)
  387.     TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
  388.  
  389.   *ptr_to_instptr = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
  390.                *ptr_to_instptr,
  391.                convert (integer_type_node, build_component_ref (aref, delta_name, 0, 0)));
  392.   return build_component_ref (aref, pfn_name, 0, 0);
  393. }
  394.  
  395. /* Build a virtual function for type TYPE.
  396.    If ASSOC is non-NULL, build the vtable starting with the intial
  397.    approximation that it is the same as the one which is the head of
  398.    the assocation list.  */
  399. static tree
  400. build_vtable (assoc, type)
  401.      tree assoc, type;
  402. {
  403.   tree name = get_vtable_name (type);
  404.   tree virtuals, decl;
  405.  
  406.   if (assoc)
  407.     {
  408.       virtuals = copy_list (ASSOC_VIRTUALS (assoc));
  409.       decl = build_decl (VAR_DECL, name, TREE_TYPE (ASSOC_VTABLE (assoc)));
  410.     }
  411.   else
  412.     {
  413.       virtuals = NULL_TREE;
  414.       decl = build_decl (VAR_DECL, name, void_type_node);
  415.     }
  416.  
  417. #ifdef GATHER_STATISTICS
  418.   n_vtables += 1;
  419.   n_vtable_elems += list_length (virtuals);
  420. #endif
  421.  
  422.   if (write_virtuals >= 2)
  423.     {
  424.       if (CLASSTYPE_INTERFACE_UNKNOWN (type) == 0)
  425.     {
  426.       TREE_PUBLIC (decl) = CLASSTYPE_VTABLE_NEEDS_WRITING (type);
  427.       TREE_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
  428.     }
  429.     }
  430.   else if (write_virtuals > 0)
  431.     TREE_PUBLIC (decl) = 1;
  432.   else if (write_virtuals < 0)
  433.     TREE_EXTERNAL (decl) = 1;
  434.  
  435.   IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl);
  436.   /* Initialize the association list for this type, based
  437.      on our first approximation.  */
  438.   CLASS_ASSOC_VTABLE (type) = decl;
  439.   CLASS_ASSOC_VIRTUALS (type) = virtuals;
  440.  
  441.   TREE_STATIC (decl) = 1;
  442.   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
  443.                DECL_ALIGN (decl));
  444.  
  445.   if (assoc && write_virtuals >= 0)
  446.     DECL_VIRTUAL_P (decl) = 1;
  447.   /* Remember which class this vtable is really for.  */
  448.   DECL_VPARENT (decl) = type;
  449.   DECL_CONTEXT (decl) = type;
  450.   CLASSTYPE_MARKED3 (type) = 1;
  451.   CLASSTYPE_MARKED4 (type) = 1;
  452.   return decl;
  453. }
  454.  
  455. /* Give TYPE a new virtual function table which is initialized
  456.    with a skeleton-copy of its original initialization.  The only
  457.    entry that changes is the `delta' entry, so we can really
  458.    share a lot of structure.
  459.  
  460.    FOR_TYPE is the derived type which caused this table to
  461.    be needed.
  462.  
  463.    ASSOC is the type association which provided TYPE for FOR_TYPE.
  464.  
  465.    The way we update BASE_ASSOC's vtable information is just to change the
  466.    association information in FOR_TYPE's association list.  */
  467. static void
  468. prepare_fresh_vtable (assoc, base_assoc, for_type)
  469.      tree assoc, base_assoc, for_type;
  470. {
  471.   tree basetype = ASSOC_TYPE (assoc);
  472.   tree orig_decl = ASSOC_VTABLE (assoc);
  473.   tree name = build_type_pathname (VTABLE_NAME_FORMAT, basetype, for_type);
  474.   tree new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
  475.   tree path;
  476.   int result;
  477.  
  478.   assert (TREE_USED (assoc) == 0);
  479.  
  480.   /* Remember which class this vtable is really for.  */
  481.   DECL_VPARENT (new_decl) = ASSOC_TYPE (base_assoc);
  482.   DECL_CONTEXT (new_decl) = for_type;
  483.  
  484.   TREE_STATIC (new_decl) = 1;
  485.   ASSOC_VTABLE (assoc) = pushdecl_top_level (new_decl);
  486.   DECL_VIRTUAL_P (new_decl) = 1;
  487.   DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
  488.  
  489.   /* Make fresh virtual list, so we can smash it later.  */
  490.   assert (ASSOC_VIRTUALS (assoc));
  491.   ASSOC_VIRTUALS (assoc) = copy_list (ASSOC_VIRTUALS (assoc));
  492.  
  493. #ifdef GATHER_STATISTICS
  494.   n_vtables += 1;
  495.   n_vtable_elems += list_length (ASSOC_VIRTUALS (assoc));
  496. #endif
  497.  
  498.   /* Set `new_decl's PUBLIC and EXTERNAL bits.  */
  499.   if (write_virtuals >= 2)
  500.     {
  501.       if (CLASSTYPE_INTERFACE_UNKNOWN (for_type) == 0)
  502.     {
  503.       TREE_PUBLIC (new_decl) = CLASSTYPE_VTABLE_NEEDS_WRITING (for_type);
  504.       TREE_EXTERNAL (new_decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (for_type);
  505.     }
  506.     }
  507.   else if (write_virtuals > 0)
  508.     TREE_PUBLIC (new_decl) = 1;
  509.   else if (write_virtuals < 0)
  510.     TREE_EXTERNAL (new_decl) = 1;
  511.  
  512.   CLASSTYPE_MARKED3 (basetype) = 1;
  513.   CLASSTYPE_MARKED4 (basetype) = 1;
  514.  
  515.   /* Mark all types between FOR_TYPE and TYPE as having been
  516.      touched, so that if we change virtual function table entries,
  517.      new vtables will be initialized.  We may reach the virtual
  518.      baseclass via ambiguous intervening baseclasses.  This
  519.      loop makes sure we get through to the actual baseclass we marked.  */
  520.  
  521.   do
  522.     {
  523.       result = get_base_distance (basetype, for_type, 0, &path);
  524.       for_type = TREE_VALUE (path);
  525.       while (path)
  526.     {
  527.       CLASSTYPE_MARKED3 (TREE_VALUE (path)) = 1;
  528.       path = TREE_CHAIN (path);
  529.     }
  530.     }
  531.   while (result == -2);
  532. }
  533.  
  534. /* Access the virtual function table entry that logically
  535.    contains BASE_FNDECL.  VIRTUALS is the virtual function table's
  536.    initializer.  */
  537. static tree
  538. get_vtable_entry (virtuals, base_fndecl)
  539.      tree virtuals, base_fndecl;
  540. {
  541.   int i = (HOST_BITS_PER_INT >= BITS_PER_WORD
  542. #ifdef VTABLE_USES_MASK
  543.        && 0
  544. #endif
  545.        ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
  546.           & ((1<<(BITS_PER_WORD-1))-1))
  547.        : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
  548.  
  549. #ifdef GATHER_STATISTICS
  550.   n_vtable_searches += i;
  551. #endif
  552.  
  553.   while (i > 0)
  554.     {
  555.       virtuals = TREE_CHAIN (virtuals);
  556.       i -= 1;
  557.     }
  558.   return virtuals;
  559. }
  560.  
  561. /* Put new entry ENTRY into virtual function table initializer
  562.    VIRTUALS.  The virtual function table is for type CONTEXT.
  563.  
  564.    Also update DECL_VINDEX (FNDECL).  */
  565.  
  566. static void
  567. modify_vtable_entry (old_entry_in_list, new_entry, fndecl, context)
  568.      tree old_entry_in_list, new_entry, fndecl, context;
  569. {
  570.   tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list));
  571.   tree vindex;
  572.  
  573.   /* We can't put in the really right offset information
  574.      here, since we have not yet laid out the class to
  575.      take into account virtual base classes.  */
  576.   TREE_VALUE (old_entry_in_list) = new_entry;
  577.   vindex = DECL_VINDEX (TREE_OPERAND (base_pfn, 0));
  578.   if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
  579.     SET_DECL_VINDEX (fndecl, vindex);
  580.   else
  581.     {
  582.       if (! tree_int_cst_equal (DECL_VINDEX (fndecl), vindex))
  583.     {
  584.       tree elts = CONSTRUCTOR_ELTS (new_entry);
  585.       tree vfield = CLASSTYPE_VFIELD (context);
  586.       /* Compute the relative offset of vtable we are really looking for.  */
  587.       TREE_VALUE (elts) = genop (PLUS_EXPR,
  588.                      build_int (DECL_OFFSET (vfield)
  589.                         / DECL_SIZE_UNIT (vfield)),
  590.                      TREE_VALUE (elts));
  591.       /* Say what index to use when we use that vtable.  */
  592. #ifndef VTABLE_USES_MASK
  593.       vindex = build_int_2 (TREE_INT_CST_LOW (vindex) & ~(1 << (BITS_PER_WORD -1)), 0);
  594. #endif
  595.       TREE_VALUE (TREE_CHAIN (elts)) = vindex;
  596.     }
  597.     }
  598. }
  599.  
  600. /* Modify virtual function tables in lattice topped by T to
  601.    place FNDECL in tables which previously held BASE_FNDECL.
  602.    PFN is just FNDECL wrapped in an ADDR_EXPR, so that it
  603.    is suitable for placement directly into an initializer.
  604.  
  605.    All distinct virtual function tables that this type uses
  606.    must be updated.  */
  607. static void
  608. modify_vtable_entries (t, fndecl, base_fndecl, pfn)
  609.      tree t;
  610.      tree fndecl, base_fndecl, pfn;
  611. {
  612.   tree base_offset, offset;
  613.   tree context = DECL_CONTEXT (base_fndecl);
  614.   tree vfield = CLASSTYPE_VFIELD (t);
  615.   tree vfields, vbases;
  616.  
  617.   DECL_VCONTEXT (fndecl) = DECL_VCONTEXT (base_fndecl);
  618.  
  619.   if (DECL_CONTEXT (fndecl) == t
  620.       || !(TYPE_USES_VIRTUAL_BASECLASSES (t)
  621.        || TYPE_USES_MULTIPLE_INHERITANCE (t)))
  622.     offset = integer_zero_node;
  623.   else
  624.     {
  625.       tree assoc = virtual_member (DECL_CONTEXT (fndecl), CLASSTYPE_VBASECLASSES (t));
  626.       if (assoc == NULL_TREE)
  627.     assoc = assoc_value (DECL_CONTEXT (fndecl), t);
  628.       assert (assoc != NULL_TREE);
  629.       offset = ASSOC_OFFSET (assoc);
  630.     }
  631.  
  632.   /* For each layer of base class (i.e., the first base class, and each
  633.      virtual base class from that one), modify the virtual function table
  634.      of the derived class to contain the new virtual function.
  635.      A class has as many vfields as it has virtual base classes (total).  */
  636.   for (vfields = CLASSTYPE_VFIELDS (t); vfields; vfields = TREE_CHAIN (vfields))
  637.     {
  638.       int normal = 1;
  639.       tree assoc, this_offset;
  640.       tree base, path;
  641.  
  642.       /* Find the right base class for this derived class, call it BASE.  */
  643.       base = TREE_VALUE (vfields);
  644.  
  645.       if (base != context)
  646.     {
  647.       /* If BASE_FNDECL is not contained in the vtable accessed by
  648.          the vslot, don't try to modify the vtable.
  649.          
  650.          Virtual functions from virtual baseclasses are not in derived
  651.          virtual function tables.  This is an implementation decision;
  652.          it keeps there from being a combinatorial exposion in the
  653.          number of different vtables which must be maintained.  */
  654.  
  655.       if (get_base_distance (base, context, 0, 0) == -1)
  656.         continue;
  657.  
  658.       /* BASE_FNDECL is defined in a class derived from
  659.          the base class owning this VFIELD.  */
  660.     }
  661.       /* Get the path starting from the deepest base class CONTEXT
  662.      of T (i.e., first defn of BASE_FNDECL).  */
  663.       get_base_distance (context, t, 0, &path);
  664.  
  665.       /* Get our best approximation of what to use for constructing
  666.      the virtual function table for T.  */
  667.       do
  668.     {
  669.       /* Walk from base toward derived, stopping at the
  670.          most derived baseclass that matters.  */
  671.       if (TREE_VIA_VIRTUAL (path))
  672.         {
  673.           base = TREE_VALUE (path);
  674.           assoc = value_member (TYPE_MAIN_VARIANT (base), CLASSTYPE_VBASECLASSES (t));
  675.           break;
  676.         }
  677.       if (TREE_CHAIN (path) == NULL_TREE
  678.           || (CLASSTYPE_BASECLASS (TREE_VALUE (TREE_CHAIN (path)), 1)
  679.           != TREE_VALUE (path))
  680.           || TREE_CHAIN (TREE_CHAIN (path)) == NULL_TREE)
  681.         {
  682.           base = TREE_VALUE (path);
  683.           assoc = assoc_value (TYPE_MAIN_VARIANT (base), t);
  684.           break;
  685.         }
  686.       path = TREE_CHAIN (path);
  687.     }
  688.       while (1);
  689.  
  690.       /* Find the right offset for the this pointer based on the base
  691.      class we just found.  */
  692.       base_offset = ASSOC_OFFSET (assoc);
  693.       if (base_offset == integer_zero_node)
  694.     this_offset = offset;
  695.       else
  696.     this_offset = genop (MINUS_EXPR, offset, base_offset);
  697.  
  698.       /* Make sure we can modify the derived association with immunity.  */
  699.       if (TREE_USED (CLASSTYPE_ASSOC (t)))
  700.     CLASSTYPE_ASSOC (t) = copy_assoc (CLASSTYPE_ASSOC (t));
  701.  
  702.       /* We call this case NORMAL iff this virtual function table
  703.      pointer field has its storage reserved in this class.
  704.      This is normally the case without virtual baseclasses
  705.      or off-center multiple baseclasses.  */
  706.       normal = (vfield != NULL_TREE
  707.         && TREE_VALUE (vfields) == DECL_FCONTEXT (vfield)
  708.         && (TREE_PURPOSE (vfields) == NULL_TREE
  709.             || ! TREE_VIA_VIRTUAL (TREE_PURPOSE (vfields))));
  710.  
  711.       if (normal && TREE_PURPOSE (vfields))
  712.     /* Everything looks normal so far...check that we are really
  713.        working from VFIELD's basetype, and not some other appearance
  714.        of that basetype in the lattice.  */
  715.     normal = (TREE_PURPOSE (vfields) == get_base_type (TREE_VALUE (vfields), t, 0));
  716.  
  717.       if (normal)
  718.     {
  719.       /* In this case, it is *type*'s vtable we are modifying.  */
  720.       context = t;
  721.       if (! CLASSTYPE_MARKED4 (t))
  722.         build_vtable (assoc, t);
  723.       assoc = CLASSTYPE_ASSOC (t);
  724.     }
  725.       else
  726.     {
  727.       /* This is our very own copy of `basetype' to play with.  */
  728.       if (! CLASSTYPE_MARKED4 (ASSOC_TYPE (assoc)))
  729.         prepare_fresh_vtable (assoc, CLASSTYPE_ASSOC (base), t);
  730.     }
  731.  
  732.       modify_vtable_entry (get_vtable_entry (ASSOC_VIRTUALS (assoc), base_fndecl),
  733.                build_vtable_entry (this_offset, pfn),
  734.                fndecl, context);
  735.     }
  736.   for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
  737.     {
  738.       tree this_offset;
  739.       tree base, path;
  740.  
  741.       if (! ASSOC_VTABLE (vbases))
  742.     /* There are only two ways that a type can fail to have
  743.        virtual functions: neither it nor any of its base
  744.        types define virtual functions (in which case
  745.        no updating need be done), or virtual functions
  746.        accessible to it come from virtual base classes
  747.        (in which case we have or will get them modified
  748.        in other passes of this loop).  */
  749.     continue;
  750.  
  751.       base = TREE_VALUE (vbases);
  752.       path = NULL_TREE;
  753.  
  754.       if (base != context
  755.       && get_base_distance (context, base, 0, &path) == -1)
  756.     continue;
  757.  
  758.       /* Doesn't matter if not actually from this virtual base class,
  759.          but shouldn't come from deeper virtual baseclasses.  The enclosing
  760.      loop should take care of such baseclasses.  */
  761.       while (path)
  762.     {
  763.       if (TREE_VIA_VIRTUAL (path))
  764.         goto skip;
  765.       path = TREE_CHAIN (path);
  766.     }
  767.  
  768.       base_offset = ASSOC_OFFSET (vbases);
  769.       if (base_offset == integer_zero_node)
  770.     this_offset = offset;
  771.       else
  772.     this_offset = genop (MINUS_EXPR, offset, base_offset);
  773.  
  774.       /* Make sure we can modify the derived association with immunity.  */
  775.       if (TREE_USED (CLASSTYPE_ASSOC (t)))
  776.     CLASSTYPE_ASSOC (t) = copy_assoc (CLASSTYPE_ASSOC (t));
  777.  
  778.       /* This is our very own copy of `basetype' to play with.  */
  779.       if (! CLASSTYPE_MARKED4 (ASSOC_TYPE (vbases)))
  780.     {
  781.       tree context_assoc = assoc_value (context, base);
  782.       prepare_fresh_vtable (vbases, context_assoc, t);
  783.     }
  784.       modify_vtable_entry (get_vtable_entry (ASSOC_VIRTUALS (vbases), base_fndecl),
  785.                build_vtable_entry (this_offset, pfn),
  786.                fndecl, context);
  787.     skip: {}
  788.     }
  789. }
  790.  
  791. static tree
  792. add_virtual_function (pending_virtuals, has_virtual, x, first)
  793.      tree pending_virtuals;
  794.      int *has_virtual;
  795.      tree x;
  796.      int first;
  797. {
  798.   int debug_vbase = 1;
  799.  
  800.   /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
  801.      convert to void *.  Make such a conversion here.  */
  802.   tree vfn = build1 (ADDR_EXPR, ptr_type_node, x);
  803.   TREE_LITERAL (vfn) = 1;
  804.   TREE_ADDRESSABLE (x) = CLASSTYPE_VTABLE_NEEDS_WRITING (current_class_type);
  805.  
  806.   /* If the virtual function is a redefinition of a prior one,
  807.      figure out in which base class the new definition goes,
  808.      and if necessary, make a fresh virtual function table
  809.      to hold that entry.  */
  810.   if (DECL_VINDEX (x) == NULL_TREE)
  811.     {
  812.       tree entry = build_vtable_entry (integer_zero_node, vfn);
  813.  
  814.       /* Build a new INT_CST for this DECL_VINDEX.  */
  815. #ifdef VTABLE_USES_MASK
  816.       SET_DECL_VINDEX (x, build_int_2 (++(*has_virtual), 0));
  817. #else
  818.       SET_DECL_VINDEX (x, build_int_2 (((1 << (BITS_PER_WORD - 1)) | ++(*has_virtual)), ~0));
  819. #endif
  820.       pending_virtuals = tree_cons (DECL_VINDEX (x), entry, pending_virtuals);
  821.     }
  822.   /* Happens if declared twice in class.  We will give error
  823.      later.  */
  824.   else if (TREE_CODE (DECL_VINDEX (x)) == INTEGER_CST)
  825.     return pending_virtuals;
  826.   else if (debug_vbase && TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
  827.     {
  828.       /* Need an entry in some other virtual function table.
  829.          Deal with this after we have laid out our virtual base classes.  */
  830.       pending_hard_virtuals = temp_tree_cons (x, vfn, pending_hard_virtuals);
  831.     }
  832.   else
  833.     {
  834.       /* Need an entry in some other virtual function table.
  835.          We can do this now.  */
  836.       tree base_fndecl_list = DECL_VINDEX (x), base_fndecls, prev = 0;
  837.       tree vtable_context = DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type));
  838.       tree true_base_fndecl = 0;
  839.  
  840.       /* First assign DECL_VINDEX from the base vfn with which
  841.      we share our vtable.  */
  842.       base_fndecls = base_fndecl_list;
  843.       while (base_fndecls)
  844.     {
  845.       if (TREE_CHAIN (base_fndecls) == NULL_TREE
  846.           || DECL_FCONTEXT (CLASSTYPE_VFIELD (DECL_CONTEXT (TREE_VALUE (base_fndecls)))) == vtable_context)
  847.         {
  848.           true_base_fndecl = TREE_VALUE (base_fndecls);
  849.           modify_vtable_entries (current_class_type, x,
  850.                      true_base_fndecl, vfn);
  851.           if (prev)
  852.         TREE_CHAIN (prev) = TREE_CHAIN (base_fndecls);
  853.           else
  854.         base_fndecl_list = prev;
  855.           break;
  856.         }
  857.       prev = base_fndecls;
  858.       base_fndecls = TREE_CHAIN (base_fndecls);
  859.     }
  860.  
  861.       /* Now fill in the rest of the vtables.  */
  862.       base_fndecls = base_fndecl_list;
  863.       while (base_fndecls)
  864.     {
  865.       /* If we haven't found one we like, first one wins.  */
  866.       if (true_base_fndecl == 0)
  867.         true_base_fndecl = TREE_VALUE (base_fndecls);
  868.  
  869.       modify_vtable_entries (current_class_type, x,
  870.                  TREE_VALUE (base_fndecls), vfn);
  871.       base_fndecls = TREE_CHAIN (base_fndecls);
  872.     }
  873.  
  874.       DECL_VCONTEXT (x) = DECL_VCONTEXT (true_base_fndecl);
  875.     }
  876.   return pending_virtuals;
  877. }
  878.  
  879. /* Obstack on which to build the vector of class methods.  */
  880. struct obstack class_obstack;
  881. extern struct obstack *current_obstack;
  882.  
  883. /* Add method METHOD to class TYPE.  This is used when a method
  884.    has been defined which did not initially appear in the class definition,
  885.    and helps cut down on spurious error messages.
  886.  
  887.    FIELDS is the entry in the METHOD_VEC vector entry of the class type where
  888.    the method should be added.  */
  889. void
  890. add_method (type, fields, method)
  891.      tree type, *fields, method;
  892. {
  893.   /* We must make a copy of METHOD here, since we must be sure that
  894.      we have exclusive title to this method's TREE_CHAIN.  */
  895.   int temp = allocation_temporary_p ();
  896.   tree decl;
  897.  
  898.   if (temp)
  899.     end_temporary_allocation ();
  900.   {
  901.     decl = copy_node (method);
  902.     if (DECL_RTL (decl) == 0)
  903.       make_function_rtl (decl);
  904.   }
  905.  
  906.   if (fields && *fields)
  907.     {
  908.       /* Take care not to hide destructor.  */
  909.       TREE_CHAIN (decl) = TREE_CHAIN (*fields);
  910.       TREE_CHAIN (*fields) = decl;
  911.     }
  912.   else if (CLASSTYPE_METHOD_VEC (type) == 0)
  913.     {
  914.       tree method_vec = make_node (TREE_VEC);
  915.       if (DECL_NAME (TYPE_NAME (type)) == DECL_ORIGINAL_NAME (decl))
  916.     {
  917.       TREE_VEC_ELT (method_vec, 0) = decl;
  918.       TREE_VEC_LENGTH (method_vec) = 1;
  919.     }
  920.       else
  921.     {
  922.       obstack_free (current_obstack, method_vec);
  923.       obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *));
  924.       TREE_VEC_ELT (method_vec, 1) = decl;
  925.       TREE_VEC_LENGTH (method_vec) = 2;
  926.       obstack_finish (current_obstack);
  927.     }
  928.       CLASSTYPE_METHOD_VEC (type) = method_vec;
  929.     }
  930.   else
  931.     {
  932.       tree method_vec = CLASSTYPE_METHOD_VEC (type);
  933.       int len = TREE_VEC_LENGTH (method_vec);
  934.  
  935.       /* Adding a new ctor or dtor.  */
  936.       if (DECL_NAME (TYPE_NAME (type)) == DECL_ORIGINAL_NAME (decl))
  937.     TREE_VEC_ELT (method_vec, 0) = decl;
  938.       else
  939.     {
  940.       tree *end = (tree *)obstack_next_free (&class_obstack);
  941.       if (end != TREE_VEC_END (method_vec))
  942.         {
  943.           tree tmp_vec = copy_node (method_vec);
  944.           obstack_copy (current_obstack, &TREE_VEC_ELT (method_vec, 1), len);
  945.           obstack_blank (current_obstack, sizeof (tree *));
  946.           obstack_finish (current_obstack);
  947.           method_vec = tmp_vec;
  948.         }
  949.       else
  950.         {
  951.           /* We can easily extend the last such method_vec created.  */
  952.           obstack_free (&class_obstack, method_vec);
  953.           obstack_blank (&class_obstack,
  954.                  ((char *)end - (char *)method_vec) + sizeof (tree *));
  955.           method_vec = (tree)obstack_base (&class_obstack);
  956.           obstack_finish (&class_obstack);
  957.         }
  958.       TREE_VEC_ELT (method_vec, len) = decl;
  959.       TREE_VEC_LENGTH (method_vec) = len + 1;
  960.       CLASSTYPE_METHOD_VEC (type) = method_vec;
  961.  
  962.       if (CLASSTYPE_BASELINK_VEC (type))
  963.         {
  964.           /* ??? May be better to know whether these can be extended?  */
  965.           tree baselink_vec = copy_node (CLASSTYPE_BASELINK_VEC (type));
  966.  
  967.           obstack_copy (current_obstack, &TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), 1), len);
  968.           TREE_VEC_ELT (baselink_vec, len) = 0;
  969.           TREE_VEC_LENGTH (baselink_vec) = len + 1;
  970.           CLASSTYPE_BASELINK_VEC (type) = baselink_vec;
  971.         }
  972.     }
  973.     }
  974.   DECL_CONTEXT (decl) = type;
  975.   DECL_VCONTEXT (decl) = type;
  976.  
  977.   if (temp)
  978.     resume_temporary_allocation ();
  979. }
  980.  
  981. /* Subroutines of finish_struct.  */
  982.  
  983. /* Look through the list of fields for this struct, deleting
  984.    duplicates as we go.  This must be recursive to handle
  985.    anonymous unions.
  986.  
  987.    FIELD is the field which may not appear anywhere in FIELDS.
  988.    FIELD_PTR, if non-null, is the starting point at which
  989.    chained deletions may take place.
  990.    The value returned is the first acceptable entry found
  991.    in FIELDS.
  992.  
  993.    Note that anonymous fields which are not of UNION_TYPE are
  994.    not duplicates, they are just anonymous fields.  This happens
  995.    when we have unnamed bitfields, for example.  */
  996. static tree
  997. delete_duplicate_fields_1 (field, field_ptr, fields)
  998.      tree field, *field_ptr, fields;
  999. {
  1000.   tree x;
  1001.   tree prev = field_ptr ? *field_ptr : 0;
  1002.   if (DECL_NAME (field) == 0)
  1003.     {
  1004.       if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
  1005.     return fields;
  1006.  
  1007.       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
  1008.     fields = delete_duplicate_fields_1 (x, field_ptr, fields);
  1009.       if (prev)
  1010.     TREE_CHAIN (prev) = fields;
  1011.       return fields;
  1012.     }
  1013.   else
  1014.     {
  1015.       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
  1016.     {
  1017.       if (DECL_NAME (x) == 0)
  1018.         {
  1019.           if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
  1020.         continue;
  1021.           TYPE_FIELDS (TREE_TYPE (x))
  1022.         = delete_duplicate_fields_1 (field, 0, TYPE_FIELDS (TREE_TYPE (x)));
  1023.           if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
  1024.         {
  1025.           if (prev == 0)
  1026.             fields = TREE_CHAIN (fields);
  1027.           else
  1028.             TREE_CHAIN (prev) = TREE_CHAIN (x);
  1029.         }
  1030.         }
  1031.       else
  1032.         {
  1033.           if (DECL_NAME (field) == DECL_NAME (x))
  1034.         {
  1035.           if (TREE_CODE (field) == CONST_DECL
  1036.               && TREE_CODE (x) == CONST_DECL)
  1037.             error_with_decl (x, "duplicate enum value `%s'");
  1038.           else if (TREE_CODE (field) == CONST_DECL
  1039.                || TREE_CODE (x) == CONST_DECL)
  1040.             error_with_decl (x, "duplicate field `%s' (as enum and non-enum)");
  1041.           else
  1042.             error_with_decl (x, "duplicate member `%s'");
  1043.           if (prev == 0)
  1044.             fields = TREE_CHAIN (fields);
  1045.           else
  1046.             TREE_CHAIN (prev) = TREE_CHAIN (x);
  1047.         }
  1048.         }
  1049.     }
  1050.     }
  1051.   return fields;
  1052. }
  1053.  
  1054. static void
  1055. delete_duplicate_fields (fields)
  1056.      tree fields;
  1057. {
  1058.   tree x;
  1059.   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
  1060.     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, &x, TREE_CHAIN (x));
  1061. }
  1062.  
  1063. /* Add OFFSET to all child types of T.
  1064.  
  1065.    OFFSET, which is a type offset, is number of bytes.
  1066.  
  1067.    Note that we don't have to worry about having two paths to the
  1068.    same base type, since this type owns its association list.  */
  1069. static void
  1070. propagate_basetype_offsets (for_type, t, offset)
  1071.      tree for_type, t;
  1072.      tree offset;
  1073. {
  1074.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (t);
  1075.  
  1076.   for (i = 1; i <= n_baselinks; i++)
  1077.     if (! CLASSTYPE_VIA_VIRTUAL (t, i))
  1078.       {
  1079.     int j;
  1080.     tree basetype = CLASSTYPE_BASECLASS (t, i);
  1081.     tree assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), for_type);
  1082.     tree delta;
  1083.  
  1084.     for (j = i+1; j <= n_baselinks; j++)
  1085.       if (! CLASSTYPE_VIA_VIRTUAL (t, j))
  1086.         {
  1087.           /* The next basetype offset must take into account the space
  1088.          between the classes, not just the size of each class.  */
  1089.           delta = genop (MINUS_EXPR,
  1090.                  CLASSTYPE_OFFSET (CLASSTYPE_BASECLASS (t, j)),
  1091.                  CLASSTYPE_OFFSET (basetype));
  1092.           break;
  1093.         }
  1094.  
  1095.     if (CLASSTYPE_OFFSET (basetype) == integer_zero_node)
  1096.       basetype = build_classtype_variant (basetype, offset, 0);
  1097.     else
  1098.       basetype = build_classtype_variant (basetype,
  1099.                           genop (PLUS_EXPR, CLASSTYPE_OFFSET (basetype), offset), 0);
  1100.     /* Now make our own copy of this base type we can munge.  */
  1101.     basetype = copy_node (basetype);
  1102.     copy_type_lang_specific (basetype);
  1103.  
  1104.     CLASSTYPE_BASECLASS (t, i) = basetype;
  1105.     ASSOC_TYPE (assoc) = basetype;
  1106.     ASSOC_OFFSET (assoc) = CLASSTYPE_OFFSET (basetype);
  1107.     TYPE_NAME (basetype) = copy_node (TYPE_NAME (basetype));
  1108.     TREE_TYPE (TYPE_NAME (basetype)) = basetype;
  1109.     DECL_OFFSET (TYPE_NAME (basetype))
  1110.       = TREE_INT_CST_LOW (CLASSTYPE_OFFSET (basetype)) * BITS_PER_UNIT;
  1111.     propagate_basetype_offsets (for_type, basetype, offset);
  1112.  
  1113.     /* Go to our next class that counts for offset propagation.  */
  1114.     i = j;
  1115.     if (i <= n_baselinks)
  1116.       offset = genop (PLUS_EXPR, offset, delta);
  1117.       }
  1118. }
  1119.  
  1120. /* Change the visibility of T::FDECL to VISIBILITY.
  1121.    Return 1 if change was legit, otherwise return 0.  */
  1122. static int
  1123. alter_visibility (t, fdecl, visibility)
  1124.      tree t;
  1125.      tree fdecl;
  1126.      enum visibility_type visibility;
  1127. {
  1128.   tree elem = purpose_member (t, DECL_VISIBILITY (fdecl));
  1129.   if (elem && TREE_VALUE (elem) != (tree)visibility)
  1130.     {
  1131.       if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
  1132.     {
  1133.       error_with_decl (TREE_TYPE (fdecl), "conflicting visibility specifications for method `%s', ignored");
  1134.     }
  1135.       else error ("conflicting visibility specifications for field `%s', ignored", IDENTIFIER_POINTER (DECL_NAME (fdecl)));
  1136.     }
  1137.   else if (TREE_PRIVATE (fdecl) && visibility != visibility_private)
  1138.     error_with_decl (fdecl, "cannot make private %s non-private");
  1139.   else if (TREE_PROTECTED (fdecl) && visibility == visibility_public)
  1140.             
  1141.     error_with_decl (fdecl, "cannot make protected %s public");
  1142.   else if (elem == NULL_TREE)
  1143.     {
  1144.       DECL_VISIBILITY (fdecl) = tree_cons (t, (tree)visibility,
  1145.                        DECL_VISIBILITY (fdecl));
  1146.       return 1;
  1147.     }
  1148.   return 0;
  1149. }
  1150.  
  1151. /* If FOR_TYPE needs to reinitialize virtual function table pointers
  1152.    for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
  1153.    Returns BASE_INIT_LIST appropriately modified.  */
  1154.  
  1155. static tree
  1156. maybe_fixup_vptrs (for_type, type, base_init_list)
  1157.      tree for_type, type, base_init_list;
  1158. {
  1159.   /* Now reinitialize any slots that don't fall under our virtual
  1160.      function table pointer.  */
  1161.   tree vfields = CLASSTYPE_VFIELDS (type);
  1162.   while (vfields)
  1163.     {
  1164.       tree basetype = get_base_type (TREE_VALUE (vfields), for_type, 0);
  1165.       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (basetype)
  1166.       && ((DECL_OFFSET (CLASSTYPE_VFIELD (basetype))
  1167.            + DECL_OFFSET (TYPE_NAME (basetype)))
  1168.           != DECL_OFFSET (CLASSTYPE_VFIELD (for_type)))
  1169.       && ((DECL_OFFSET (CLASSTYPE_VFIELD (basetype))
  1170.            + DECL_OFFSET (TYPE_NAME (basetype)))
  1171.           != (DECL_OFFSET (CLASSTYPE_VFIELD (type))
  1172.           + DECL_OFFSET (TYPE_NAME (type)))))
  1173.     base_init_list = tree_cons (error_mark_node, basetype,
  1174.                     base_init_list);
  1175.       vfields = TREE_CHAIN (vfields);
  1176.     }
  1177.   return base_init_list;
  1178. }
  1179.  
  1180. /* If TYPE does not have a constructor, then the compiler must
  1181.    manually deal with all of the initialization this type requires.
  1182.  
  1183.    If a base initializer exists only to fill in the virtual function
  1184.    table pointer, then we mark that fact with the TREE_VIRTUAL bit.
  1185.    This way, we avoid multiple initializations of the same field by
  1186.    each virtual function table up the class hierarchy.
  1187.  
  1188.    Virtual base class pointers are not initialized here.  They are
  1189.    initialized only at the "top level" of object creation.  If we
  1190.    initialized them here, we would have to skip a lot of work.  */
  1191.  
  1192. static void
  1193. build_class_init_list (type)
  1194.      tree type;
  1195. {
  1196.   tree base_init_list = NULL_TREE;
  1197.   tree member_init_list = NULL_TREE;
  1198.  
  1199.   /* Since we build member_init_list and base_init_list using
  1200.      tree_cons, backwards fields the all through work.  */
  1201.   tree x;
  1202.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (type);
  1203.  
  1204.   for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
  1205.     {
  1206.       if (TREE_CODE (x) != FIELD_DECL)
  1207.     continue;
  1208.  
  1209.       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
  1210.       || DECL_INITIAL (x) != NULL_TREE)
  1211.     member_init_list = tree_cons (x, type, member_init_list);
  1212.     }
  1213.   member_init_list = nreverse (member_init_list);
  1214.  
  1215.   /* We will end up doing this last.  Need special marker
  1216.      to avoid infinite regress.  */
  1217.   if (TYPE_VIRTUAL_P (type))
  1218.     {
  1219.       base_init_list = build_tree_list (error_mark_node, type);
  1220.       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
  1221.     TREE_VALUE (base_init_list) = NULL_TREE;
  1222.       TREE_ADDRESSABLE (base_init_list) = 1;
  1223.     }
  1224.  
  1225.   /* Each base class which needs to have initialization
  1226.      of some kind gets to make such requests known here.  */
  1227.   for (i = n_baseclasses; i > 0; i--)
  1228.     {
  1229.       tree basetype = CLASSTYPE_BASECLASS (type, i);
  1230.       tree blist;
  1231.  
  1232.       /* Don't initialize virtual baseclasses this way.  */
  1233.       if (TREE_VIA_VIRTUAL (basetype))
  1234.     continue;
  1235.  
  1236.       if (TYPE_HAS_CONSTRUCTOR (basetype))
  1237.     {
  1238.       /* ...and the last shall come first...  */
  1239.       base_init_list = maybe_fixup_vptrs (type, basetype, base_init_list);
  1240.       base_init_list = tree_cons (NULL_TREE, basetype,
  1241.                       base_init_list);
  1242.       continue;
  1243.     }
  1244.  
  1245.       if ((blist = CLASSTYPE_BASE_INIT_LIST (basetype)) == NULL_TREE)
  1246.     /* Nothing to initialize.  */
  1247.     continue;
  1248.  
  1249.       /* ...ditto...  */
  1250.       base_init_list = maybe_fixup_vptrs (type, basetype, base_init_list);
  1251.  
  1252.       /* This is normally true for single inheritance.
  1253.      The win is we can shrink the chain of initializations
  1254.      to be done by only converting to the actual type
  1255.      we are interested in.  */
  1256.       if (TREE_VALUE (blist)
  1257.       && TREE_CODE (TREE_VALUE (blist)) == RECORD_TYPE
  1258.       && (DECL_OFFSET (TYPE_NAME (basetype))
  1259.           == DECL_OFFSET (TYPE_NAME (TREE_VALUE (blist)))))
  1260.     {
  1261.       if (base_init_list)
  1262.         {
  1263.           /* Does it do more than just fill in a
  1264.          virtual function table pointer?  */
  1265.           if (! TREE_ADDRESSABLE (blist))
  1266.         base_init_list = build_tree_list (blist, base_init_list);
  1267.           /* Can we get by just with the virtual function table
  1268.          pointer that it fills in?  */
  1269.           else if (TREE_ADDRESSABLE (base_init_list)
  1270.                && TREE_VALUE (base_init_list) == 0)
  1271.         base_init_list = blist;
  1272.           /* Maybe, but it is not obvious as the previous case.  */
  1273.           else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
  1274.         {
  1275.           tree last = tree_last (base_init_list);
  1276.           while (TREE_VALUE (last)
  1277.              && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
  1278.             last = tree_last (TREE_VALUE (last));
  1279.           if (TREE_VALUE (last) == 0)
  1280.             base_init_list = build_tree_list (blist, base_init_list);
  1281.         }
  1282.         }
  1283.       else
  1284.         base_init_list = blist;
  1285.     }
  1286.       else
  1287.     {
  1288.       /* The function expand_aggr_init knows how to do the
  1289.          initialization of `basetype' without getting
  1290.          an explicit `blist'.  */
  1291.       if (base_init_list)
  1292.         base_init_list = tree_cons (NULL_TREE, basetype, base_init_list);
  1293.       else
  1294.         base_init_list = CLASSTYPE_AS_LIST (basetype);
  1295.     }
  1296.     }
  1297.  
  1298.   if (base_init_list)
  1299.     if (member_init_list)
  1300.       CLASSTYPE_BASE_INIT_LIST (type) = build_tree_list (base_init_list, member_init_list);
  1301.     else
  1302.       CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
  1303.   else if (member_init_list)
  1304.     CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
  1305. }
  1306.  
  1307. struct base_info
  1308. {
  1309.   int has_virtual;
  1310.   int max_has_virtual;
  1311.   int n_ancestors;
  1312.   tree vfield;
  1313.   tree vfields;
  1314.   char needs_default_ctor;
  1315.   char cant_have_default_ctor;
  1316.   char needs_const_ctor;
  1317.   char cant_have_const_ctor;
  1318. };
  1319.  
  1320. /* Record information about type T derived from its base classes.
  1321.    Store most of that information in T itself, and place the
  1322.    remaining information in the struct BASE_INFO.
  1323.  
  1324.    Returns the index of the first base class to have virtual functions,
  1325.    or zero if no such base class.  */
  1326.  
  1327. static int
  1328. finish_base_struct (t, b)
  1329.      tree t;
  1330.      struct base_info *b;
  1331. {
  1332.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1333.   int first_vfn_base_index = 0;
  1334.  
  1335.   bzero (b, sizeof (struct base_info));
  1336.  
  1337.   for (i = 1; i <= n_baseclasses; i++)
  1338.     {
  1339.       tree basetype = CLASSTYPE_BASECLASS (t, i);
  1340.  
  1341.       /* If the type of basetype is incomplete, then
  1342.      we already complained about that fact
  1343.      (and we should have fixed it up as well).  */
  1344.       if (TYPE_SIZE (basetype) == 0)
  1345.     {
  1346.       int j;
  1347.       /* The base type is of incomplete type.  It is
  1348.          probably best to pretend that it does not
  1349.          exist.  */
  1350.       if (i == n_baseclasses)
  1351.         CLASSTYPE_BASECLASS (t, i) = NULL_TREE;
  1352.       CLASSTYPE_N_BASECLASSES (t) -= 1;
  1353.       n_baseclasses -= 1;
  1354.       for (j = i; j < n_baseclasses; j++)
  1355.         {
  1356.           CLASSTYPE_BASECLASS (t, j) = CLASSTYPE_BASECLASS (t, j+1);
  1357.           SET_CLASSTYPE_VIAS (t, j,
  1358.                   CLASSTYPE_VIA_PUBLIC (t, j+1),
  1359.                   CLASSTYPE_VIA_VIRTUAL (t, j+1));
  1360.         }
  1361.     }
  1362.  
  1363.       if (TYPE_WRAP_TYPE (t) == NULL_TREE)
  1364.     TYPE_WRAP_TYPE (t) = TYPE_WRAP_TYPE (basetype);
  1365.       else if (TYPE_WRAP_TYPE (basetype)
  1366.            && TYPE_WRAP_TYPE (t) != TYPE_WRAP_TYPE (basetype))
  1367.     /* Must have its own.  */
  1368.     TYPE_WRAP_TYPE (t) = error_mark_node;
  1369.  
  1370.       if (TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
  1371.     b->needs_default_ctor = 1;
  1372.       else if (TYPE_HAS_CONSTRUCTOR (basetype))
  1373.     b->cant_have_default_ctor = 1;
  1374.       if (TYPE_GETS_CONST_INIT_REF (basetype))
  1375.     b->needs_const_ctor = 1;
  1376.       else if (TYPE_GETS_INIT_REF (basetype))
  1377.     b->cant_have_const_ctor = 1;
  1378.  
  1379.       CLASSTYPE_ALTERS_VISIBILITIES_P (t)
  1380.     |= CLASSTYPE_ALTERS_VISIBILITIES_P (basetype);
  1381.  
  1382.       b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
  1383.       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
  1384.       TYPE_NEEDS_CONSTRUCTOR (t) |= TYPE_NEEDS_CONSTRUCTOR (basetype);
  1385.       TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
  1386.       TYPE_ANY_ASSIGNS_THIS (t) |= TYPE_ANY_ASSIGNS_THIS (basetype);
  1387.       TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (basetype);
  1388.       TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (basetype);
  1389.  
  1390.       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
  1391.       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
  1392.  
  1393.       if (CLASSTYPE_OFFSET (basetype) != integer_zero_node)
  1394.     {
  1395.       /* Completely unshare potentially shared data, and
  1396.          update what is ours.  */
  1397.       tree assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), t);
  1398.       basetype = copy_node (basetype);
  1399.       copy_type_lang_specific (basetype);
  1400.       CLASSTYPE_BASECLASS (t, i) = basetype;
  1401.       ASSOC_TYPE (assoc) = basetype;
  1402.  
  1403.       /* Propagate this offset through all the children.  Do this
  1404.          before uniquizing baseclasses for virtual functions.  */
  1405.       CLASSTYPE_ASSOC (basetype) = copy_assoc (CLASSTYPE_ASSOC (TYPE_MAIN_VARIANT (basetype)));
  1406.  
  1407.       propagate_basetype_offsets (basetype, basetype, CLASSTYPE_OFFSET (basetype));
  1408.     }
  1409.  
  1410.       if (! CLASSTYPE_VIA_VIRTUAL (t, i))
  1411.     CLASSTYPE_N_SUPERCLASSES (t) += 1;
  1412.  
  1413.       if (TYPE_VIRTUAL_P (basetype))
  1414.     {
  1415.       if (CLASSTYPE_VSIZE (basetype) > b->max_has_virtual)
  1416.         b->max_has_virtual = CLASSTYPE_VSIZE (basetype);
  1417.  
  1418.       /* Don't borrow virtuals from virtual baseclasses.  */
  1419.       if (TREE_VIA_VIRTUAL (basetype))
  1420.         continue;
  1421.  
  1422.       if (first_vfn_base_index == 0)
  1423.         {
  1424.           first_vfn_base_index = i;
  1425.  
  1426.           b->has_virtual = CLASSTYPE_VSIZE (basetype);
  1427.           b->vfield = CLASSTYPE_VFIELD (basetype);
  1428.           b->vfields = CLASSTYPE_VFIELDS (basetype);
  1429.           CLASSTYPE_VFIELD (t) = b->vfield;
  1430.         }
  1431.       else
  1432.         {
  1433.           /* Only add unique vfields, and flatten them out as we go.  */
  1434.           tree vfields = CLASSTYPE_VFIELDS (basetype);
  1435.           while (vfields)
  1436.         {
  1437.           if (TREE_PURPOSE (vfields) == NULL_TREE
  1438.               || ! TREE_VIA_VIRTUAL (TREE_PURPOSE (vfields)))
  1439.             {
  1440.               tree value = TREE_VALUE (vfields);
  1441.               if (TYPE_MAIN_VARIANT (basetype) == value)
  1442.             b->vfields = tree_cons (basetype, value, b->vfields);
  1443.               else
  1444.             b->vfields = tree_cons (get_base_type (value, basetype, 0),
  1445.                         value, b->vfields);
  1446.               TREE_TYPE (b->vfields) = basetype;
  1447.             }
  1448.           vfields = TREE_CHAIN (vfields);
  1449.         }
  1450.  
  1451.           if (b->has_virtual == 0)
  1452.         {
  1453.           first_vfn_base_index = i;
  1454.           b->has_virtual = CLASSTYPE_VSIZE (basetype);
  1455.           b->vfield = CLASSTYPE_VFIELD (basetype);
  1456.           CLASSTYPE_VFIELD (t) = b->vfield;
  1457.         }
  1458.         }
  1459.     }
  1460.     }
  1461.   if (b->vfield == 0)
  1462.     /* If all virtual functions come only from virtual baseclasses.  */
  1463.     return 0;
  1464.   return first_vfn_base_index;
  1465. }
  1466.  
  1467. static int
  1468. typecode_p (type, code)
  1469.      tree type;
  1470.      enum tree_code code;
  1471. {
  1472.   return (TREE_CODE (type) == code
  1473.       || (TREE_CODE (type) == REFERENCE_TYPE
  1474.           && TREE_CODE (TREE_TYPE (type)) == code));
  1475. }
  1476.  
  1477. /* Set memoizing fields and bits of T (and its variants) for later use.
  1478.    FIRST_VFN_BASE_INDEX is the first baseclass of T with virtual functions.
  1479.    MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables.  */
  1480. static void
  1481. finish_struct_bits (t, first_vfn_base_index, max_has_virtual)
  1482.      tree t;
  1483.      int first_vfn_base_index, max_has_virtual;
  1484. {
  1485.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1486.   tree method_vec = CLASSTYPE_METHOD_VEC (t);
  1487.  
  1488.   /* Fix up variants (if any).  */
  1489.   tree variants = TYPE_NEXT_VARIANT (t);
  1490.   while (variants)
  1491.     {
  1492.       TYPE_NEEDS_CONSTRUCTOR (variants) = TYPE_NEEDS_CONSTRUCTOR (t);
  1493.       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
  1494.       TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
  1495.       variants = TYPE_NEXT_VARIANT (variants);
  1496.     }
  1497.  
  1498.   if (n_baseclasses && max_has_virtual)
  1499.     {
  1500.       /* Done by `finish_struct' for classes without baseclasses.  */
  1501.       int has_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
  1502.       if (has_abstract_virtuals == 0)
  1503.     for (i = CLASSTYPE_N_BASECLASSES (t); i >= 1; i--)
  1504.       has_abstract_virtuals
  1505.         |= (CLASSTYPE_ABSTRACT_VIRTUALS (CLASSTYPE_BASECLASS (t, i)) != 0);
  1506.       if (has_abstract_virtuals)
  1507.     CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
  1508.     }
  1509.  
  1510.   if (n_baseclasses)
  1511.     {
  1512.       /* Notice whether this class has type conversion functions defined.
  1513.      Also report whether joining two types yields an ambiguity in the
  1514.      virtual function table, e.g.,
  1515.      
  1516.      struct A { virtual int f (); };
  1517.      struct B { virtual int f (); };
  1518.      struct C : A, B { / * no f (); * / };    / / error, ambiguous
  1519.      */
  1520.       tree basetypes = CLASSTYPE_VBASECLASSES (t), basetype;
  1521.       int n_vbases = list_length (basetypes), j;
  1522.  
  1523.       build_mi_virtuals (n_baseclasses + (n_vbases*n_baseclasses), max_has_virtual);
  1524.       /* Fill in virutal function table with values which do not come
  1525.      "normal"ly, i.e., those which come from virtual and/or
  1526.      non-leftmost base classes.  */
  1527.       for (i = n_baseclasses; basetypes; basetypes = TREE_CHAIN (basetypes))
  1528.     {
  1529.       basetype = TREE_VALUE (basetypes);
  1530.       if (CLASSTYPE_VSIZE (basetype))
  1531.         for (j = n_baseclasses; j > 0; j--)
  1532.           {
  1533.         tree this_base = CLASSTYPE_BASECLASS (t, j);
  1534.         if (get_base_distance (basetype, TYPE_MAIN_VARIANT (this_base), 0, 0) != -1)
  1535.           add_mi_virtuals (++i, TREE_CHAIN (ASSOC_VIRTUALS (basetypes)));
  1536.           }
  1537.     }
  1538.       for (i = n_baseclasses; i > 0; i--)
  1539.     {
  1540.       basetype = CLASSTYPE_BASECLASS (t, i);
  1541.  
  1542.       if (TYPE_HAS_CONVERSION (basetype))
  1543.         {
  1544.           TYPE_HAS_CONVERSION (t) = 1;
  1545.           TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype);
  1546.           TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype);
  1547.         }
  1548.       if (TREE_VIA_VIRTUAL (basetype))
  1549.         /* Virtual functions from virtual baseclasses are done above.  */;
  1550.       else if (i == first_vfn_base_index)
  1551.         add_mi_virtuals (i, TREE_CHAIN (CLASS_ASSOC_VIRTUALS (t)));
  1552.       else if (CLASSTYPE_VSIZE (basetype) != 0)
  1553.         add_mi_virtuals (i, TREE_CHAIN (CLASS_ASSOC_VIRTUALS (basetype)));
  1554.     }
  1555.       report_ambiguous_mi_virtuals (n_baseclasses + (n_vbases*n_baseclasses), t);
  1556. #if 0
  1557.       /* Now that we know what the virtual functiond table looks like,
  1558.      fix up offsets in the presence of virtual base classes.  */
  1559.       if (n_vbases)
  1560.     fixup_vbase_offsets (t);
  1561. #endif
  1562.     }
  1563.  
  1564.   /* Need to test METHOD_VEC here in case all methods
  1565.      (conversions and otherwise) are inherited.  */
  1566.   if (TYPE_HAS_CONVERSION (t) && method_vec != NULL_TREE)
  1567.     {
  1568.       tree first_conversions[last_conversion_type];
  1569.       tree last_conversions[last_conversion_type];
  1570.       enum conversion_type conv_index;
  1571.       tree *tmp;
  1572.       int i;
  1573.  
  1574.       bzero (first_conversions, sizeof (first_conversions));
  1575.       for (tmp = &TREE_VEC_ELT (method_vec, 1);
  1576.        tmp != TREE_VEC_END (method_vec); tmp += 1)
  1577.     {
  1578.       if (OPERATOR_TYPENAME_P (DECL_ORIGINAL_NAME (*tmp)))
  1579.         {
  1580.           tree fntype = TREE_TYPE (*tmp);
  1581.           tree return_type = TREE_TYPE (fntype);
  1582.           assert (TREE_CODE (fntype) == METHOD_TYPE);
  1583.  
  1584.           if (typecode_p (return_type, POINTER_TYPE))
  1585.         {
  1586.           if (TREE_READONLY (TREE_TYPE (return_type)))
  1587.             conv_index = constptr_conv;
  1588.           else
  1589.             conv_index = ptr_conv;
  1590.         }
  1591.           else if (typecode_p (return_type, INTEGER_TYPE))
  1592.         {
  1593.           TYPE_HAS_INT_CONVERSION (t) = 1;
  1594.           conv_index = int_conv;
  1595.         }
  1596.           else if (typecode_p (return_type, REAL_TYPE))
  1597.         {
  1598.           TYPE_HAS_REAL_CONVERSION (t) = 1;
  1599.           conv_index = real_conv;
  1600.         }
  1601.           else
  1602.         continue;
  1603.  
  1604.           if (first_conversions[(int) conv_index] == NULL_TREE)
  1605.         first_conversions[(int) conv_index] = *tmp;
  1606.           last_conversions[(int) conv_index] = *tmp;
  1607.         }
  1608.     }
  1609.  
  1610.       for (i = 0; i < (int) last_conversion_type; i++)
  1611.     if (first_conversions[i] != last_conversions[i])
  1612.       CLASSTYPE_CONVERSION (t, i) = error_mark_node;
  1613.     else
  1614.       CLASSTYPE_CONVERSION (t, i) = first_conversions[i];
  1615.     }
  1616.  
  1617.   /* If this type has constructors, force its mode to be BLKmode,
  1618.      and force its TREE_ADDRESSABLE bit to be nonzero.  */
  1619.   if (TYPE_NEEDS_CONSTRUCTING (t) || TYPE_NEEDS_DESTRUCTOR (t))
  1620.     {
  1621.       tree variants = t;
  1622.  
  1623.       if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  1624.     DECL_MODE (TYPE_NAME (t)) = BLKmode;
  1625.       while (variants)
  1626.     {
  1627.       TYPE_MODE (variants) = BLKmode;
  1628.       TREE_ADDRESSABLE (variants) = 1;
  1629.       variants = TYPE_NEXT_VARIANT (variants);
  1630.     }
  1631.     }
  1632. }
  1633.  
  1634. /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
  1635.    (or C++ class declaration).
  1636.  
  1637.    For C++, we must handle the building of derived classes.
  1638.    Also, C++ allows static class members.  The way that this is
  1639.    handled is to keep the field name where it is (as the DECL_NAME
  1640.    of the field), and place the overloaded decl in the DECL_OFFSET
  1641.    of the field.  layout_record and layout_union will know about this.
  1642.  
  1643.    More C++ hair: inline functions have text in their
  1644.    DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
  1645.    meaningful tree structure.  After the struct has been laid out, set
  1646.    things up so that this can happen.
  1647.  
  1648.    And still more: virtual functions.  In the case of single inheritance,
  1649.    when a new virtual function is seen which redefines a virtual function
  1650.    from the base class, the new virtual function is placed into
  1651.    the virtual function table at exactly the same address that
  1652.    it had in the base class.  When this is extended to multiple
  1653.    inheritance, the same thing happens, except that multiple virtual
  1654.    function tables must be maintained.  The first virtual function
  1655.    table is treated in exactly the same way as in the case of single
  1656.    inheritance.  Additional virtual function tables have different
  1657.    DELTAs, which tell how to adjust `this' to point to the right thing.
  1658.  
  1659.    LIST_OF_FIELDLISTS is just that.  The elements of the list are
  1660.    TREE_LIST elements, whose TREE_PURPOSE field tells what visibility
  1661.    the list has, and the TREE_VALUE slot gives the actual fields.
  1662.  
  1663.    EMPTY is non-zero if this structure has no declarations following it.
  1664.  
  1665.    If flag_all_virtual == 1, then we lay all functions into
  1666.    the virtual function table, as though they were declared
  1667.    virtual.  Constructors do not lay down in the virtual function table.
  1668.  
  1669.    If flag_all_virtual == 2, then we lay all functions into
  1670.    the virtual function table, such that virtual functions
  1671.    occupy a space by themselves, and then all functions
  1672.    of the class occupy a space by themselves.  This is illustrated
  1673.    in the following diagram:
  1674.  
  1675.    class A; class B : A;
  1676.  
  1677.     Class A's vtbl:            Class B's vtbl:
  1678.     --------------------------------------------------------------------
  1679.    | A's virtual functions|        | B's virtual funcitions    |
  1680.    |              |        | (may inherit some from A).    |
  1681.     --------------------------------------------------------------------
  1682.    | All of A's functions |        | All of A's functions        |
  1683.    | (such as a->A::f).      |        | (such as b->A::f)        |
  1684.     --------------------------------------------------------------------
  1685.                     | B's new virtual functions    |
  1686.                     | (not defined in A.)        |
  1687.                      -------------------------------
  1688.                     | All of B's functions        |
  1689.                     | (such as b->B::f)        |
  1690.                      -------------------------------
  1691.  
  1692.    this allows the program to make references to any function, virtual
  1693.    or otherwise in a type-consistant manner.  */
  1694.  
  1695. tree
  1696. finish_struct (t, list_of_fieldlists, empty, warn_anon)
  1697.      tree t;
  1698.      tree list_of_fieldlists;
  1699.      int empty;
  1700.      int warn_anon;
  1701. {
  1702.   extern int interface_only, interface_unknown;
  1703.   int old;
  1704.   int round_up_size = 1;
  1705.   /* Set non-zero to debug using default functions.
  1706.      Not set by program.  */
  1707.   static int debug_default_functions = 0;
  1708.  
  1709.   enum tree_code code = TREE_CODE (t);
  1710.   register tree x, y, method_vec;
  1711.   int needs_ctor = 0, needs_dtor = 0;
  1712.   int members_need_dtors = 0;
  1713.   tree name = TYPE_NAME (t), fields, fn_fields, tail;
  1714.   enum visibility_type visibility;
  1715.   int all_virtual;
  1716.   int has_virtual;
  1717.   int max_has_virtual;
  1718.   tree pending_virtuals = NULL_TREE;
  1719.   tree abstract_virtuals = NULL_TREE;
  1720.   tree vfield;
  1721.   tree vfields;
  1722.   int needs_default_ctor;
  1723.   int cant_have_default_ctor;
  1724.   int needs_const_ctor;
  1725.   int cant_have_const_ctor;
  1726.  
  1727.   /* The index of the first base class which has virtual
  1728.      functions.  Only applied to non-virtual baseclasses.  */
  1729.   int first_vfn_base_index;
  1730.  
  1731.   int i, n_baseclasses;
  1732.   int any_default_members = 0;
  1733.   char *err_name;
  1734.   int const_sans_init = 0;
  1735.   int ref_sans_init = 0;
  1736.   int nonprivate_method = 0;
  1737.  
  1738.   if (TREE_CODE (name) == TYPE_DECL)
  1739.     {
  1740.       extern int lineno;
  1741.  
  1742.       DECL_SOURCE_FILE (name) = input_filename;
  1743.       DECL_SOURCE_LINE (name) = lineno;
  1744.       name = DECL_NAME (name);
  1745.     }
  1746.   err_name = IDENTIFIER_POINTER (name);
  1747.  
  1748.   if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (name))
  1749.     {
  1750.       warning ("un-usable class ignored (anonymous classes and unions are useless)");
  1751.       err_name = "(anon)";
  1752.     }
  1753.  
  1754.   leftmost_baseclasses = NULL_TREE;
  1755.   if (TYPE_SIZE (t))
  1756.     {
  1757.       if (TREE_CODE (t) == UNION_TYPE)
  1758.     error ("redefinition of `union %s'", err_name);
  1759.       else if (TREE_CODE (t) == RECORD_TYPE)
  1760.     error ("redefinition of `struct %s'", err_name);
  1761.       else
  1762.     assert (0);
  1763.       popclass (0);
  1764.       return t;
  1765.     }
  1766.  
  1767. #ifdef FIELD_XREF
  1768.   FIELD_xref_decl(current_function_decl,t);
  1769. #endif
  1770.  
  1771.   /* If this type was previously laid out as a forward reference,
  1772.      make sure we lay it out again.  */
  1773.  
  1774.   TYPE_SIZE (t) = 0;
  1775.   CLASSTYPE_GOT_SEMICOLON (t) = 0;
  1776.   CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
  1777.   CLASSTYPE_INTERFACE_UNKNOWN (t) = interface_unknown;
  1778.  
  1779.   old = suspend_momentary ();
  1780.  
  1781.   /* Install struct as DECL_FIELD_CONTEXT of each field decl.
  1782.      Also process specified field sizes.
  1783.      Set DECL_SIZE_UNIT to the specified size, or 0 if none specified.
  1784.      The specified size is found in the DECL_INITIAL.
  1785.      Store 0 there, except for ": 0" fields (so we can find them
  1786.      and delete them, below).  */
  1787.  
  1788.   n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1789.   if (n_baseclasses >= 1)
  1790.     {
  1791.       struct base_info base_info;
  1792.  
  1793.       /* If using multiple inheritance, this may cause variants of our
  1794.      basetypes to be used (instead of their canonical forms).  */
  1795.       fields = layout_basetypes (t);
  1796.       y = tree_last (fields);
  1797.  
  1798.       first_vfn_base_index = finish_base_struct (t, &base_info);
  1799.       has_virtual = base_info.has_virtual;
  1800.       max_has_virtual = base_info.max_has_virtual;
  1801.       CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
  1802.       vfield = base_info.vfield;
  1803.       vfields = base_info.vfields;
  1804.       needs_default_ctor = base_info.needs_default_ctor;
  1805.       cant_have_default_ctor = base_info.cant_have_default_ctor;
  1806.       needs_const_ctor = base_info.needs_const_ctor;
  1807.       cant_have_const_ctor = base_info.cant_have_const_ctor;
  1808.       n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1809.     }
  1810.   else
  1811.     {
  1812.       first_vfn_base_index = 0;
  1813.       has_virtual = 0;
  1814.       max_has_virtual = 0;
  1815.       vfield = NULL_TREE;
  1816.       vfields = NULL_TREE;
  1817.       fields = NULL_TREE;
  1818.       y = NULL_TREE;
  1819.       needs_default_ctor = 0;
  1820.       cant_have_default_ctor = 0;
  1821.       needs_const_ctor = 0;
  1822.       cant_have_const_ctor = 0;
  1823.     }
  1824.  
  1825.   if (write_virtuals == 3 && ! CLASSTYPE_INTERFACE_UNKNOWN (t))
  1826.     {
  1827.       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
  1828.       CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! interface_only;
  1829.     }
  1830.  
  1831.   /* The three of these are approximations which may later be
  1832.      modified.  Needed at this point to make add_virtual_function
  1833.      and modify_vtable_entries work.  */
  1834.   CLASSTYPE_ASSOC (t) = make_assoc (integer_zero_node, t,
  1835.                     0, 0, CLASSTYPE_ASSOC (t));
  1836.   CLASSTYPE_VFIELDS (t) = vfields;
  1837.   CLASSTYPE_VFIELD (t) = vfield;
  1838.  
  1839.   fn_fields = NULL_TREE;
  1840.   tail = NULL_TREE;
  1841.   if (y && list_of_fieldlists)
  1842.     TREE_CHAIN (y) = TREE_VALUE (list_of_fieldlists);
  1843.  
  1844. #ifdef SOS
  1845.   if (flag_all_virtual == 2)
  1846.     all_virtual = 2;
  1847.   else
  1848. #endif
  1849.     {
  1850.       if (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (t))
  1851.     all_virtual = 1;
  1852.       else
  1853.     all_virtual = 0;
  1854.     }
  1855.  
  1856.   if (CLASSTYPE_DECLARED_CLASS (t) == 0)
  1857.     {
  1858.       nonprivate_method = 1;
  1859.       if (list_of_fieldlists
  1860.       && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
  1861.     TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_public;
  1862.     }
  1863.   else if (list_of_fieldlists
  1864.        && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
  1865.     TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_private;
  1866.  
  1867.   while (list_of_fieldlists)
  1868.     {
  1869.       visibility = (enum visibility_type)TREE_PURPOSE (list_of_fieldlists);
  1870.  
  1871.       for (x = TREE_VALUE (list_of_fieldlists); x; x = TREE_CHAIN (x))
  1872.     {
  1873.       TREE_PRIVATE (x) = visibility == visibility_private;
  1874.       TREE_PROTECTED (x) = visibility == visibility_protected;
  1875. #ifdef FIELD_XREF
  1876.       FIELD_xref_member(current_class_name,x);
  1877. #endif
  1878.  
  1879.       if (TREE_CODE (x) == FUNCTION_DECL)
  1880.         {
  1881.           /* Clear out this flag.
  1882.  
  1883.              @@ Doug may figure out how to break
  1884.          @@ this with nested classes and friends.  */
  1885.           DECL_IN_AGGR_P (x) = 0;
  1886.  
  1887.           nonprivate_method |= ! TREE_PRIVATE (x);
  1888.  
  1889.           /* If this was an evil function, don't keep it in class.  */
  1890.           if (IDENTIFIER_ERROR_LOCUS (DECL_NAME (x)))
  1891.         continue;
  1892.  
  1893.           if (y) TREE_CHAIN (y) = TREE_CHAIN (x);
  1894.           if (! fn_fields) fn_fields = x;
  1895.           else TREE_CHAIN (tail) = x;
  1896.           tail = x;
  1897.           if (DECL_CONTEXT (x))
  1898.         continue;
  1899.  
  1900.           DECL_CONTEXT (x) = t;
  1901.           DECL_VCONTEXT (x) = t;
  1902.  
  1903.           DECL_SIZE_UNIT (x) = 0;
  1904.  
  1905.           /* The name of the field is the original field name
  1906.          Save this in auxiliary field for later overloading.  */
  1907.           if (DECL_VIRTUAL_P (x)
  1908.           || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x)))
  1909.         {
  1910.           pending_virtuals = add_virtual_function (pending_virtuals,
  1911.                                &has_virtual, x,
  1912.                                first_vfn_base_index);
  1913.           if (DECL_ABSTRACT_VIRTUAL_P (x))
  1914.             abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
  1915.         }
  1916.           continue;
  1917.         }
  1918.  
  1919.       /* Handle visibility declarations.  */
  1920.       if (DECL_NAME (x) && TREE_CODE (DECL_NAME (x)) == SCOPE_REF)
  1921.         {
  1922.           tree fdecl = TREE_OPERAND (DECL_NAME (x), 1);
  1923.  
  1924.           if (y) TREE_CHAIN (y) = TREE_CHAIN (x);
  1925.           /* Make type T see field decl FDECL with
  1926.          the visibility VISIBILITY.  */
  1927.           if (TREE_CODE (fdecl) == TREE_LIST)
  1928.         {
  1929.           fdecl = TREE_VALUE (fdecl);
  1930.           while (fdecl)
  1931.             {
  1932.               if (alter_visibility (t, fdecl, visibility) == 0)
  1933.             break;
  1934.               fdecl = TREE_CHAIN (fdecl);
  1935.             }
  1936.         }
  1937.           else alter_visibility (t, fdecl, visibility);
  1938.           CLASSTYPE_ALTERS_VISIBILITIES_P (t) = 1;
  1939.           continue;
  1940.         }
  1941.  
  1942.       /* Perform error checking that did not get done in grokdeclarator.  */
  1943.       if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
  1944.         {
  1945.           error_with_decl (x, "field `%s' invalidly declared function type");
  1946.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  1947.         }
  1948.       else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
  1949.         {
  1950.           error_with_decl (x, "field `%s' invalidly declared method type");
  1951.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  1952.         }
  1953.       else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
  1954.         {
  1955.           error_with_decl (x, "field `%s' invalidly declared offset type");
  1956.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  1957.         }
  1958.       /* If this is of reference type, check if it needs an init.  */
  1959.       if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE
  1960.           && DECL_INITIAL (x) == 0)
  1961.         ref_sans_init = 1;
  1962.  
  1963.       /* When this goes into scope, it will be a non-local reference.  */
  1964.       TREE_NONLOCAL (x) = 1;
  1965.  
  1966.       if (TREE_CODE (x) == FIELD_DECL)
  1967.         {
  1968.           /* Never let anything with uninheritable virutals
  1969.          make it through without complaint.  */
  1970.           if (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
  1971.           && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (x)))
  1972.         abstract_virtuals_error (x, TREE_TYPE (x));
  1973.  
  1974.           if (TYPE_LANG_SPECIFIC (TREE_TYPE (x)))
  1975.         {
  1976.           if (TYPE_HAS_DEFAULT_CONSTRUCTOR (TREE_TYPE (x)))
  1977.             needs_default_ctor = 1;
  1978.           if (TYPE_GETS_CONST_INIT_REF (TREE_TYPE (x)))
  1979.             needs_const_ctor = 1;
  1980.           else if (TYPE_GETS_INIT_REF (TREE_TYPE (x)))
  1981.             cant_have_const_ctor = 1;
  1982.         }
  1983.           else if (DECL_INITIAL (x) == NULL_TREE
  1984.                && (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (x))
  1985.                || TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE))
  1986.         cant_have_default_ctor = 1;
  1987.  
  1988.           /* If any field is const, the structure type is pseudo-const.  */
  1989.           if (TREE_READONLY (x))
  1990.         {
  1991.           C_TYPE_FIELDS_READONLY (t) = 1;
  1992.           if (DECL_INITIAL (x) == 0)
  1993.             const_sans_init = 1;
  1994.         }
  1995.           else 
  1996.         {
  1997.           /* A field that is pseudo-const makes the structure likewise.  */
  1998.           tree t1 = TREE_TYPE (x);
  1999.           while (TREE_CODE (t1) == ARRAY_TYPE)
  2000.             t1 = TREE_TYPE (t1);
  2001.           if (IS_AGGR_TYPE (t1))
  2002.             {
  2003.               if (C_TYPE_FIELDS_READONLY (t1))
  2004.             C_TYPE_FIELDS_READONLY (t) = 1;
  2005.               if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
  2006.             const_sans_init = 1;
  2007.             }
  2008.         }
  2009.         }
  2010.       else if (TREE_STATIC (x) && TREE_CODE (t) == UNION_TYPE)
  2011.         /* Unions cannot have static members.  */
  2012.         error_with_decl (x, "field `%s' declared static in union");
  2013.  
  2014.       if (! fields) fields = x;
  2015.       DECL_FIELD_CONTEXT (x) = t;
  2016.       DECL_SIZE_UNIT (x) = 0;
  2017.  
  2018.       if (TREE_PACKED (x))
  2019.         {
  2020.           /* Invalid bit-field size done by grokfield.  */
  2021.           /* Detect invalid bit-field type.  */
  2022.           if (DECL_INITIAL (x)
  2023.           && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
  2024.           && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
  2025.         {
  2026.           error_with_decl (x, "bit-field `%s' has invalid type");
  2027.           DECL_INITIAL (x) = NULL;
  2028.         }
  2029.           if (DECL_INITIAL (x) && pedantic
  2030.           && TREE_TYPE (x) != integer_type_node
  2031.           && TREE_TYPE (x) != unsigned_type_node)
  2032.         warning_with_decl (x, "bit-field `%s' type invalid in ANSI C");
  2033.  
  2034.           /* Detect and ignore out of range field width.  */
  2035.           if (DECL_INITIAL (x))
  2036.         {
  2037.           register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  2038.  
  2039.           if (width < 0)
  2040.             {
  2041.               DECL_INITIAL (x) = NULL;
  2042.               warning_with_decl (x, "negative width in bit-field `%s'");
  2043.             }
  2044.           else if (width == 0 && DECL_NAME (x) != 0)
  2045.             {
  2046.               error_with_decl (x, "zero width for bit-field `%s'");
  2047.               DECL_INITIAL (x) = NULL;
  2048.             }
  2049.           else if (width > TYPE_PRECISION (TREE_TYPE (x)))
  2050.             {
  2051.               DECL_INITIAL (x) = NULL;
  2052.               warning_with_decl (x, "width of `%s' exceeds its type");
  2053.             }
  2054.         }
  2055.  
  2056.           /* Process valid field width.  */
  2057.           if (DECL_INITIAL (x))
  2058.         {
  2059.           register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  2060.  
  2061.           if (width == 0)
  2062.             {
  2063.               /* field size 0 => mark following field as "aligned" */
  2064.               if (TREE_CHAIN (x))
  2065.             DECL_ALIGN (TREE_CHAIN (x))
  2066.               = MAX (DECL_ALIGN (TREE_CHAIN (x)), EMPTY_FIELD_BOUNDARY);
  2067.               /* field of size 0 at the end => round up the size.  */
  2068.               else
  2069.             round_up_size = EMPTY_FIELD_BOUNDARY;
  2070.             }
  2071.           else
  2072.             {
  2073.               DECL_INITIAL (x) = NULL_TREE;
  2074.               DECL_SIZE_UNIT (x) = width;
  2075.               TREE_PACKED (x) = 1;
  2076.               /* Traditionally a bit field is unsigned
  2077.              even if declared signed.  */
  2078.               if (flag_traditional
  2079.               && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE)
  2080.             TREE_TYPE (x) = unsigned_type_node;
  2081.             }
  2082.         }
  2083.           else
  2084.         /* Non-bit-fields are aligned for their type.  */
  2085.         DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
  2086.         }
  2087.       else if (TREE_CODE (x) == FIELD_DECL)
  2088.         {
  2089.           tree type = TREE_TYPE (x);
  2090.           if (TREE_CODE (type) == ARRAY_TYPE)
  2091.         type = TREE_TYPE (type);
  2092.           if (code == UNION_TYPE)
  2093.         {
  2094.           if (TYPE_NEEDS_CONSTRUCTING (type))
  2095.             error ("member %s::%s with constructor not allowed in union",
  2096.                IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (DECL_NAME (x)));
  2097.           if (TYPE_NEEDS_DESTRUCTOR (type))
  2098.             error ("member %s::%s with destructor (also) not allowed in union",
  2099.                IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (DECL_NAME (x)));
  2100.         }
  2101.           else if (code == RECORD_TYPE)
  2102.         {
  2103.           /* Array of record type doesn't matter for this bit.  */
  2104.           TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
  2105.           if (IS_AGGR_TYPE (type))
  2106.             {
  2107.               needs_ctor |= TYPE_NEEDS_CONSTRUCTOR (type);
  2108.               needs_dtor |= TYPE_NEEDS_DESTRUCTOR (type);
  2109.               members_need_dtors |= TYPE_NEEDS_DESTRUCTOR (type);
  2110.               TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (type);
  2111.               TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (type);
  2112.               TYPE_GETS_CONST_INIT_REF (t) |= TYPE_GETS_CONST_INIT_REF (type);
  2113.             }
  2114.         }
  2115.           if (DECL_INITIAL (x) != NULL_TREE)
  2116.         {
  2117.           /* `build_class_init_list' does not recognize non-FIELD_DECLs.  */
  2118.           if (code == UNION_TYPE && any_default_members != 0)
  2119.             error ("multiple fields in union initialized");
  2120.           any_default_members = 1;
  2121.         }
  2122.         }
  2123.       y = x;
  2124.     }
  2125.       list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
  2126.       /* link the tail while we have it! */
  2127.       if (y)
  2128.     {
  2129.       TREE_CHAIN (y) = NULL_TREE;
  2130.  
  2131.       if (list_of_fieldlists
  2132.           && TREE_VALUE (list_of_fieldlists)
  2133.           && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL)
  2134.         TREE_CHAIN (y) = TREE_VALUE (list_of_fieldlists);
  2135.     }
  2136.     }
  2137.  
  2138.   if (tail) TREE_CHAIN (tail) = NULL_TREE;
  2139.  
  2140.   /* If this type has any constant members which did not come
  2141.      with their own initialization, mark that fact here.  It is
  2142.      not an error here, since such types can be saved either by their
  2143.      constructors, or by fortuitous initialization.  */
  2144.   CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
  2145.   CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
  2146.   CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
  2147.  
  2148.   if (vfield == 0
  2149.       && (has_virtual
  2150. #ifdef SOS
  2151.       || TYPE_DYNAMIC (t)
  2152. #endif
  2153.       ))
  2154.     {
  2155.       /* We build this decl with ptr_type_node, and
  2156.      change the type when we know what it should be.  */
  2157.       vfield = build_decl (FIELD_DECL, get_vfield_name (t), ptr_type_node);
  2158.       CLASSTYPE_VFIELD (t) = vfield;
  2159.       DECL_VIRTUAL_P (vfield) = 1;
  2160.       DECL_FIELD_CONTEXT (vfield) = t;
  2161.       SET_DECL_FCONTEXT (vfield, t);
  2162.       DECL_SIZE_UNIT (vfield) = 0;
  2163.       if (y)
  2164.     {
  2165.       assert (TREE_CHAIN (y) == 0);
  2166.       TREE_CHAIN (y) = vfield;
  2167.       y = vfield;
  2168.     }
  2169.       else fields = vfield;
  2170.       vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
  2171.     }
  2172.  
  2173.   /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
  2174.      And they have already done their work.
  2175.  
  2176.      C++: maybe we will support default field initialization some day...  */
  2177.  
  2178.   /* Delete all zero-width bit-fields from the front of the fieldlist */
  2179.   while (fields && TREE_PACKED (fields)
  2180.      && DECL_INITIAL (fields))
  2181.     fields = TREE_CHAIN (fields);
  2182.   /* Delete all such fields from the rest of the fields.  */
  2183.   for (x = fields; x;)
  2184.     {
  2185.       if (TREE_CHAIN (x) && TREE_PACKED (TREE_CHAIN (x))
  2186.       && DECL_INITIAL (TREE_CHAIN (x)))
  2187.     TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
  2188.       else x = TREE_CHAIN (x);
  2189.     }
  2190.   /* Delete all duplicate fields from the fields */
  2191.   delete_duplicate_fields (fields);
  2192.  
  2193.   /* Now we have the final fieldlist for the data fields.  Record it,
  2194.      then lay out the structure or union (including the fields).  */
  2195.  
  2196.   TYPE_FIELDS (t) = fields;
  2197.  
  2198.   /* If there's a :0 field at the end, round the size to the
  2199.      EMPTY_FIELD_BOUNDARY.  */
  2200.   TYPE_ALIGN (t) = round_up_size;
  2201.  
  2202.   if (debug_default_functions)
  2203.     {
  2204.       if ((TYPE_NEEDS_CONSTRUCTOR (t) || TYPE_HAS_CONSTRUCTOR (t) || needs_ctor)
  2205.       && ! TYPE_HAS_INIT_REF (t))
  2206.     {
  2207.       tree default_fn = cons_up_default_function (t, name, 1);
  2208.       TREE_CHAIN (default_fn) = fn_fields;
  2209.       DECL_CONTEXT (default_fn) = t;
  2210.       DECL_VCONTEXT (default_fn) = t;
  2211.       fn_fields = default_fn;
  2212.       TYPE_HAS_INIT_REF (t) = 1;
  2213.       default_fn = cons_up_default_function (t, name, 3);
  2214.       TREE_CHAIN (default_fn) = fn_fields;
  2215.       DECL_CONTEXT (default_fn) = t;
  2216.       DECL_VCONTEXT (default_fn) = t;
  2217.       fn_fields = default_fn;
  2218.       nonprivate_method = 1;
  2219.     }
  2220.  
  2221.       if (! TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
  2222.       && needs_default_ctor && ! cant_have_default_ctor)
  2223.     {
  2224.       tree default_fn = cons_up_default_function (t, name, 2);
  2225.       TREE_CHAIN (default_fn) = fn_fields;
  2226.       DECL_CONTEXT (default_fn) = t;
  2227.       DECL_VCONTEXT (default_fn) = t;
  2228.       fn_fields = default_fn;
  2229.       TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
  2230.       nonprivate_method = 1;
  2231.     }
  2232.     }
  2233.   /* Warn about duplicate methods in fn_fields.  Also compact
  2234.      method lists so that lookup can be made faster.
  2235.  
  2236.      Algorithm:  Outer loop builds lists by method name.
  2237.      Inner loop checks for redundant method names within a list.
  2238.  
  2239.      Data Structure:  List of method lists.  The outer list
  2240.      is a TREE_LIST, whose TREE_PURPOSE field is the field name
  2241.      and the TREE_VALUE is the TREE_CHAIN of the FUNCTION_DECLs.
  2242.      Friends are chained in the same way as member functions, but
  2243.      they live in the TREE_TYPE field of the outer list.
  2244.      That allows them to be quicky deleted, and requires
  2245.      no extra storage.
  2246.  
  2247.      If there are any constructors/destructors, they are moved to
  2248.      the front of the list.  This makes pushclass more efficient.
  2249.  
  2250.      We also link each field which has shares a name with its
  2251.      baseclass to the head of the list of fields for that base class.
  2252.      This allows us to reduce search time in places like `build_method_call'
  2253.      to consider only reasonably likely functions.  */
  2254.  
  2255.   if (fn_fields)
  2256.     {
  2257.       /* Now prepare to gather fn_fields into vector.  */
  2258.       struct obstack *ambient_obstack = current_obstack;
  2259.       current_obstack = &class_obstack;
  2260.       method_vec = make_node (TREE_VEC);
  2261.       /* Room has been saved for constructors and destructors.  */
  2262.       current_obstack = ambient_obstack;
  2263.       /* Now make this a live vector.  */
  2264.       obstack_free (&class_obstack, method_vec);
  2265.       obstack_blank (&class_obstack, sizeof (struct tree_vec));
  2266.  
  2267.       while (fn_fields)
  2268.     {
  2269.       /* NEXT Pointer, TEST Pointer, and BASE Pointer.  */
  2270.       tree nextp, *testp;
  2271.  
  2272.       nextp = TREE_CHAIN (fn_fields);
  2273.       TREE_CHAIN (fn_fields) = NULL_TREE;
  2274.       /* Constrcutors are handled easily in search routines.
  2275.          Besides, we know we wont find any, so do not bother looking.  */
  2276.       if (DECL_ORIGINAL_NAME (fn_fields) == name
  2277.           && TREE_VEC_ELT (method_vec, 0) == 0)
  2278.         TREE_VEC_ELT (method_vec, 0) = fn_fields;
  2279.       else
  2280.         {
  2281.           testp = &TREE_VEC_ELT (method_vec, 0);
  2282.           if (*testp == NULL_TREE)
  2283.         testp++;
  2284.           while ((int)testp < (int)obstack_next_free (&class_obstack)
  2285.              && DECL_ORIGINAL_NAME (*testp) != DECL_ORIGINAL_NAME (fn_fields))
  2286.         testp++;
  2287.           if ((int)testp < (int)obstack_next_free (&class_obstack))
  2288.         {
  2289.           for (x = *testp; x; x = TREE_CHAIN (x))
  2290.             {
  2291.               if (DECL_NAME (fn_fields) == DECL_NAME (x))
  2292.             {
  2293.               /* We complain about multiple destructors on sight,
  2294.                  so we do not repeat the warning here.  Friend-friend
  2295.                  ambiguities are warned about outside this loop.  */
  2296.               if (! DESTRUCTOR_NAME_P (DECL_NAME (fn_fields)))
  2297.                 error_with_file_and_line (DECL_SOURCE_FILE (fn_fields),
  2298.                               DECL_SOURCE_LINE (fn_fields),
  2299.                               "ambiguous method `%s' in structure",
  2300.                               lang_printable_name (fn_fields));
  2301.               break;
  2302.             }
  2303.               y = x;
  2304.             }
  2305.           if (x == 0)
  2306.             if (*testp)
  2307.               TREE_CHAIN (y) = fn_fields;
  2308.             else
  2309.               *testp = fn_fields;
  2310.         }
  2311.           else
  2312.         {
  2313.           obstack_ptr_grow (&class_obstack, fn_fields);
  2314.           method_vec = (tree)obstack_base (&class_obstack);
  2315.         }
  2316.         }
  2317.       fn_fields = nextp;
  2318.     }
  2319.  
  2320.       TREE_VEC_LENGTH (method_vec)
  2321.     = (tree *)obstack_next_free (&class_obstack) - (&TREE_VEC_ELT (method_vec, 0));
  2322.       obstack_finish (&class_obstack);
  2323.       CLASSTYPE_METHOD_VEC (t) = method_vec;
  2324.  
  2325.       if (nonprivate_method == 0
  2326.       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2327.       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  2328.     {
  2329.       for (i = 0; i <= n_baseclasses; i++)
  2330.         if (CLASSTYPE_VIA_PUBLIC (t, i))
  2331.           {
  2332.         nonprivate_method = 1;
  2333.         break;
  2334.           }
  2335.       if (nonprivate_method == 0)
  2336.         warning ("all class member functions are private");
  2337.     }
  2338.     }
  2339.   else
  2340.     {
  2341.       method_vec = 0;
  2342.  
  2343.       /* Just in case these got accidently
  2344.      filled in by syntax errors.  */
  2345.       TYPE_HAS_CONSTRUCTOR (t) = 0;
  2346.       TYPE_HAS_DESTRUCTOR (t) = 0;
  2347.     }
  2348.  
  2349.   /* If there are constructors (and destructors), they are at the
  2350.      front.  Place destructors at very front.  Also warn if all
  2351.      constructors and/or destructors are private (in which case this
  2352.      class is effectively unusable.  */
  2353.   if (TYPE_HAS_DESTRUCTOR (t))
  2354.     {
  2355.       tree dtor, prev;
  2356.  
  2357.       for (dtor = TREE_VEC_ELT (method_vec, 0); dtor; prev = dtor, dtor = TREE_CHAIN (dtor))
  2358.     {
  2359.       if (DESTRUCTOR_NAME_P (DECL_NAME (dtor)))
  2360.         {
  2361.           if (TREE_PRIVATE (dtor)
  2362.           && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2363.           && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  2364.         warning_with_decl (TYPE_NAME (t), "class `%s' only defines a private destructor and has no friends");
  2365.           break;
  2366.         }
  2367.     }
  2368.       /* Wild parse errors can cause this to happen.  */
  2369.       if (dtor == NULL_TREE)
  2370.     TYPE_HAS_DESTRUCTOR (t) = 0;
  2371.       else if (dtor != TREE_VEC_ELT (method_vec, 0))
  2372.     {
  2373.       TREE_CHAIN (prev) = TREE_CHAIN (dtor);
  2374.       TREE_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
  2375.       TREE_VEC_ELT (method_vec, 0) = dtor;
  2376.     }
  2377.     }
  2378.   else if (members_need_dtors
  2379.        || TYPE_USES_VIRTUAL_BASECLASSES (t)
  2380.        || TYPE_USES_MULTIPLE_INHERITANCE (t))
  2381.     {
  2382.       /* Here we must cons up a destructor on the fly.  */
  2383.       tree dtor = cons_up_default_function (t, name, 0);
  2384.  
  2385.       /* If we couldn't make it work, then pretend we didn't need it.  */
  2386.       if (dtor == void_type_node)
  2387.     TYPE_NEEDS_DESTRUCTOR (t) = 0;
  2388.       else
  2389.     {
  2390.       DECL_CONTEXT (dtor) = t;
  2391.       DECL_VCONTEXT (dtor) = t;
  2392.       if (DECL_VIRTUAL_P (dtor))
  2393.         pending_virtuals = add_virtual_function (pending_virtuals,
  2394.                              &has_virtual, dtor);
  2395.       if (TYPE_HAS_CONSTRUCTOR (t))
  2396.         TREE_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
  2397.       else if (method_vec == 0)
  2398.         {
  2399.           /* Now prepare to gather fn_fields into vector.  */
  2400.           struct obstack *ambient_obstack = current_obstack;
  2401.           current_obstack = &class_obstack;
  2402.           method_vec = make_node (TREE_VEC);
  2403.           /* Room has been saved for constructors and destructors.  */
  2404.           current_obstack = ambient_obstack;
  2405.           TREE_VEC_LENGTH (method_vec) = 1;
  2406.           CLASSTYPE_METHOD_VEC (t) = method_vec;
  2407.         }
  2408.       TREE_VEC_ELT (method_vec, 0) = dtor;
  2409.       TYPE_HAS_DESTRUCTOR (t) = 1;
  2410.     }
  2411.     }
  2412.   if (TYPE_HAS_CONSTRUCTOR (t)
  2413.       && ! CLASSTYPE_DECLARED_EXCEPTION (t)
  2414.       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2415.       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  2416.     {
  2417.       int nonprivate_ctor = 0;
  2418.       tree ctor;
  2419.  
  2420.       for (ctor = TREE_VEC_ELT (method_vec, 0); ctor; ctor = TREE_CHAIN (ctor))
  2421.     if (! TREE_PRIVATE (ctor))
  2422.       {
  2423.         nonprivate_ctor = 1;
  2424.         break;
  2425.       }
  2426.       if (nonprivate_ctor == 0)
  2427.     warning ("class %s only defines private constructors and has no friends",
  2428.          TYPE_NAME_STRING (t));
  2429.     }
  2430.  
  2431.   /* Now for each member function (except for constructors and
  2432.      destructors), compute where member functions of the same
  2433.      name reside in base classes.  */
  2434.   if (n_baseclasses != 0
  2435.       && method_vec != NULL_TREE
  2436.       && TREE_VEC_LENGTH (method_vec) > 1)
  2437.     {
  2438.       int len = TREE_VEC_LENGTH (method_vec);
  2439.       tree baselink_vec = make_tree_vec (len);
  2440.       int any_links = 0;
  2441.  
  2442.       for (i = 1; i < len; i++)
  2443.     {
  2444.       TREE_VEC_ELT (baselink_vec, i)
  2445.         = get_baselinks (t, DECL_ORIGINAL_NAME (TREE_VEC_ELT (method_vec, i)));
  2446.       if (TREE_VEC_ELT (baselink_vec, i) != 0)
  2447.         any_links = 1;
  2448.     }
  2449.       if (any_links != 0)
  2450.     CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
  2451.       else
  2452.     obstack_free (current_obstack, baselink_vec);
  2453.     }
  2454.  
  2455.   /* We can't know this information until we have seen all of the
  2456.      constructors.  */
  2457.   TYPE_NONE_ASSIGN_THIS (t) = 0;
  2458.  
  2459.   /* Pass layout information about base classes to layout_type, if any.  */
  2460.  
  2461.   if (n_baseclasses)
  2462.     {
  2463.       tree pseudo_basetype = TREE_TYPE (base_layout_decl);
  2464.  
  2465.       TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t);
  2466.       TYPE_FIELDS (t) = base_layout_decl;
  2467.  
  2468.       TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t);
  2469.       TYPE_SIZE_UNIT (pseudo_basetype) = TYPE_SIZE_UNIT (t);
  2470.       TYPE_MODE (pseudo_basetype) = TYPE_MODE (t);
  2471.       TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t);
  2472.       DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype);
  2473.     }
  2474.  
  2475.   layout_type (t);
  2476.  
  2477.   if (n_baseclasses)
  2478.     TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
  2479.  
  2480.   /* C++: do not let empty structures exist.  */
  2481.   if (integer_zerop (TYPE_SIZE (t)))
  2482.     TYPE_SIZE (t) = TYPE_SIZE (char_type_node);
  2483.  
  2484.   /* Set the TYPE_DECL for this type to contain the right
  2485.      value for DECL_OFFSET, so that we can use it as part
  2486.      of a COMPONENT_REF for multiple inheritance.  */
  2487.  
  2488.   if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  2489.     layout_decl (TYPE_NAME (t));
  2490.  
  2491.   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
  2492.     {
  2493.       tree vbases;
  2494.  
  2495.       max_has_virtual = layout_vbasetypes (t, max_has_virtual);
  2496.       vbases = CLASSTYPE_VBASECLASSES (t);
  2497.       CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
  2498.  
  2499.       /* Now fix up any virtual base class types that we
  2500.      left lying around.  We must get these done
  2501.      before we try to lay out the virtual function table.  */
  2502.       pending_hard_virtuals = nreverse (pending_hard_virtuals);
  2503. #if 1
  2504.       /* This loop makes all the entries in the virtual function tables
  2505.      of interest contain the "latest" version of the functions
  2506.      we have defined.  */
  2507.  
  2508.       while (vbases)
  2509.     {
  2510.       tree virtuals = ASSOC_VIRTUALS (vbases);
  2511.  
  2512.       if (virtuals)
  2513.         virtuals = TREE_CHAIN (virtuals);
  2514.  
  2515.       while (virtuals != NULL_TREE)
  2516.         {
  2517.           tree pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
  2518.           tree base_fndecl = TREE_OPERAND (pfn, 0);
  2519.           tree decl = get_first_matching_virtual (t, base_fndecl, 0);
  2520.           tree context = DECL_CONTEXT (decl);
  2521.           if (decl != base_fndecl && context != t)
  2522.         {
  2523.           tree assoc = NULL_TREE, these_virtuals;
  2524.           int i = TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)) & ((1<<(BITS_PER_WORD-1))-1);
  2525.  
  2526.           if (TYPE_USES_VIRTUAL_BASECLASSES (context))
  2527.             assoc = virtual_member (DECL_CONTEXT (base_fndecl),
  2528.                         CLASSTYPE_VBASECLASSES (context));
  2529.           if (assoc == NULL_TREE)
  2530.             assoc = assoc_value (DECL_CONTEXT (base_fndecl), context);
  2531.           if (assoc != NULL_TREE)
  2532.             {
  2533.               these_virtuals = ASSOC_VIRTUALS (assoc);
  2534.  
  2535.               while (i-- > 0)
  2536.             these_virtuals = TREE_CHAIN (these_virtuals);
  2537.               pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (these_virtuals));
  2538.               modify_vtable_entries (t, decl, base_fndecl, pfn);
  2539.             }
  2540.         }
  2541.           virtuals = TREE_CHAIN (virtuals);
  2542.         }
  2543.       vbases = TREE_CHAIN (vbases);
  2544.     }
  2545. #endif /* 1 */
  2546.       while (pending_hard_virtuals)
  2547.     {
  2548.       /* Need an entry in some other virtual function table.  */
  2549.       tree base_fndecls = DECL_VINDEX (TREE_PURPOSE (pending_hard_virtuals));
  2550.       while (base_fndecls)
  2551.         {
  2552.           modify_vtable_entries (t, TREE_PURPOSE (pending_hard_virtuals),
  2553.                      TREE_VALUE (base_fndecls),
  2554.                      TREE_VALUE (pending_hard_virtuals));
  2555.           base_fndecls = TREE_CHAIN (base_fndecls);
  2556.         }
  2557.       pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
  2558.     }
  2559.     }
  2560.   else
  2561.     CLASSTYPE_VBASE_SIZE (t) = integer_zero_node;
  2562.  
  2563.   if (pending_virtuals)
  2564.     {
  2565.       pending_virtuals = nreverse (pending_virtuals);
  2566.       /* We must enter these virtuals into the table.  */
  2567.       if (first_vfn_base_index == 0)
  2568.     {
  2569.       pending_virtuals = tree_cons (NULL_TREE, the_null_vtable_entry,
  2570.                     pending_virtuals);
  2571.       build_vtable (0, t);
  2572.     }
  2573.       else
  2574.     {
  2575.       /* Here we know enough to change the type of our virtual
  2576.          function table, but we will wait until later this function.  */
  2577.       if (! CLASSTYPE_MARKED4 (t))
  2578.         build_vtable (assoc_value (TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (t, first_vfn_base_index)), t), t);
  2579.     }
  2580.  
  2581.       /* If this type has basetypes with constructors, then those
  2582.      constructors might clobber the virtual function table.  But
  2583.      they don't if the derived class shares the exact vtable of the base
  2584.      class.  */
  2585.  
  2586.       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
  2587.     }
  2588.   else if (first_vfn_base_index)
  2589.     {
  2590.       tree basetype = get_base_type (DECL_FIELD_CONTEXT (vfield), t, 0);
  2591.       tree assoc;
  2592.       
  2593.       if (TREE_VIA_VIRTUAL (basetype))
  2594.     assoc = virtual_member (DECL_FIELD_CONTEXT (vfield), CLASSTYPE_VBASECLASSES (t));
  2595.       else
  2596.     assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), t);
  2597.  
  2598.       /* This class contributes nothing new to the virtual function
  2599.      table.  However, it may have declared functions which
  2600.      went into the virtual function table "inherited" from the
  2601.      base class.  If so, we grab a copy of those updated functions,
  2602.      and pretend they are ours.  */
  2603.  
  2604. #ifdef SOS
  2605.       /* Don't define this ahead of time if we have more
  2606.      fields to add later.  */
  2607.       if (all_virtual == 2 && fn_fields != NULL_TREE)
  2608.     ;
  2609.       else
  2610. #endif
  2611.     {
  2612.       /* See if we should steal the virtual info from base class.  */
  2613.       if (CLASS_ASSOC_VTABLE (t) == NULL_TREE)
  2614.         CLASS_ASSOC_VTABLE (t) = ASSOC_VTABLE (assoc);
  2615.       if (CLASS_ASSOC_VIRTUALS (t) == NULL_TREE)
  2616.         CLASS_ASSOC_VIRTUALS (t) = ASSOC_VIRTUALS (assoc);
  2617.     }
  2618.       if (CLASS_ASSOC_VTABLE (t) != ASSOC_VTABLE (assoc))
  2619.     CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
  2620.     }
  2621.  
  2622.   if (has_virtual > max_has_virtual)
  2623.     max_has_virtual = has_virtual;
  2624.   if (max_has_virtual || first_vfn_base_index)
  2625.     {
  2626. #ifdef VTABLE_USES_MASK
  2627.       if (max_has_virtual >= VINDEX_MAX)
  2628.     {
  2629.       error ("too many virtual functions for class `%s' (VINDEX_MAX < %d)", TYPE_NAME_STRING (t), has_virtual);
  2630.     }
  2631. #endif
  2632.       TYPE_VIRTUAL_P (t) = 1;
  2633.       CLASSTYPE_VSIZE (t) = has_virtual;
  2634.       if (first_vfn_base_index)
  2635.     {
  2636.       if (pending_virtuals)
  2637.         CLASS_ASSOC_VIRTUALS (t) = chainon (CLASS_ASSOC_VIRTUALS (t),
  2638.                         pending_virtuals);
  2639.     }
  2640.       else if (has_virtual)
  2641.     {
  2642.       CLASS_ASSOC_VIRTUALS (t) = pending_virtuals;
  2643.       if (write_virtuals >= 0)
  2644.         DECL_VIRTUAL_P (CLASS_ASSOC_VTABLE (t)) = 1;
  2645.     }
  2646.     }
  2647.  
  2648. #ifdef SOS
  2649.   if (all_virtual == 2 && (max_has_virtual || method_vec))
  2650.     {
  2651.       /* Now that we know the size of the virtual table, lay out
  2652.      the absolute table following it.  */
  2653.       int i;
  2654.       tree tmp;
  2655.       tree pending_absolutes = NULL_TREE;
  2656.       int has_absolute = has_virtual;
  2657.  
  2658.       /* Local variables for building a table filled with strings
  2659.      containing the names of interesting things.  */
  2660.       tree decl, init;
  2661.       tree start = NULL_TREE, next = NULL_TREE;
  2662.       tree *outer = &TREE_VEC_ELT (method_vec, 0);
  2663.  
  2664.       while (outer != TREE_VEC_END (method_vec))
  2665.     {
  2666.       tree inner;
  2667.       for (inner = *outer; inner; inner = TREE_CHAIN (inner))
  2668.         {
  2669.           tree entry;
  2670.           tree fn;
  2671.  
  2672.           /* Don't bother with functions which appear
  2673.          for visibility reasons.  */
  2674.           if (DECL_FIELD_CONTEXT (inner) != t)
  2675.         continue;
  2676.  
  2677.           /* Must lay this function into its absolute table as well.
  2678.          This forces an inline function to be written out.  */
  2679.           fn = build1 (ADDR_EXPR, ptr_type_node, inner);
  2680.           TREE_LITERAL (fn) = 1;
  2681.           DECL_DINDEX (inner) = build_int_2 (++has_absolute, 0);
  2682.           entry = build_vtable_entry (integer_zero_node, fn);
  2683.           pending_absolutes = tree_cons (DECL_DINDEX (inner), entry,
  2684.                          pending_absolutes);
  2685.         }
  2686.       outer++;
  2687.     }
  2688.  
  2689.       CLASS_ASSOC_VIRTUALS (t) = chainon (CLASS_ASSOC_VIRTUALS (t),
  2690.                       nreverse (pending_absolutes));
  2691.       if (TYPE_DYNAMIC (t))
  2692.     {
  2693.       for (outer = &TREE_VEC_ELT (method_vec, 0);
  2694.            outer != TREE_VEC_END (method_vec);
  2695.            outer++)
  2696.         {
  2697.           tree inner;
  2698.           for (inner = *outer; inner; inner = TREE_CHAIN (inner))
  2699.         {
  2700.           tree str = make_node (STRING_CST);
  2701.           TREE_STRING_LENGTH (str) = IDENTIFIER_LENGTH (DECL_NAME (inner));
  2702.           TREE_STRING_POINTER (str) = IDENTIFIER_POINTER (DECL_NAME (inner));
  2703.           TREE_LITERAL (str) = 1;
  2704.           TREE_STATIC (str) = 1;
  2705.           TREE_TYPE (str)
  2706.             = build_cplus_array_type (char_type_node,
  2707.                           build_index_type (build_int_2 (TREE_STRING_LENGTH (str) - 1, 0)));
  2708.           
  2709.           if (start)
  2710.             {
  2711.               TREE_CHAIN (next) = build_tree_list (NULL_TREE, str);
  2712.               next = TREE_CHAIN (next);
  2713.             }
  2714.           else
  2715.             {
  2716.               start = build_tree_list (NULL_TREE, str);
  2717.               next = start;
  2718.             }
  2719.         }
  2720.         }
  2721.  
  2722.       /* Lay out dynamic link table for SOS.  */
  2723.  
  2724.       decl = finish_table (get_linktable_name (t),
  2725.                    string_type_node, start, 0);
  2726.     }
  2727.       has_virtual = has_absolute;
  2728.       CLASSTYPE_VSIZE (t) = has_virtual;
  2729.       if (has_virtual > max_has_virtual)
  2730.     max_has_virtual = has_virtual;
  2731.       if (vfield == 0)
  2732.     {
  2733.       /* We build this decl with ptr_type_node, and
  2734.          change the type when we know what it should be.  */
  2735.       vfield = build_decl (FIELD_DECL, get_vfield_name (t), ptr_type_node);
  2736.       CLASSTYPE_VFIELD (t) = vfield;
  2737.       DECL_VIRTUAL_P (vfield) = 1;
  2738.       DECL_FIELD_CONTEXT (vfield) = t;
  2739.       SET_DECL_FCONTEXT (vfield, t);
  2740.       DECL_SIZE_UNIT (vfield) = 0;
  2741.       y = tree_last (fields);
  2742.       if (y)
  2743.         TREE_CHAIN (y) = vfield;
  2744.       else
  2745.         fields = vfield;
  2746.       vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
  2747.     }
  2748.     }
  2749. #endif
  2750.  
  2751.   /* Now lay out the virtual function table.  */
  2752.   if (has_virtual)
  2753.     {
  2754.       tree atype, itype;
  2755.  
  2756.       if (TREE_TYPE (vfield) == ptr_type_node)
  2757.     {
  2758.       /* We must create a pointer to this table because
  2759.          the one inherited from base class does not exist.
  2760.          We will fill in the type when we know what it
  2761.          should really be.  */
  2762.       itype = build_index_type (build_int_2 (has_virtual, 0));
  2763.       atype = build_array_type (vtable_entry_type, itype);
  2764.       layout_type (atype);
  2765.       TREE_TYPE (vfield) = build_pointer_type (atype);
  2766.     }
  2767.       else
  2768.     {
  2769.       atype = TREE_TYPE (TREE_TYPE (vfield));
  2770.  
  2771.       if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))))
  2772.         {
  2773.           /* We must extend (or create) the boundaries on this array,
  2774.          because we picked up virtual functions from multiple
  2775.          base classes.  */
  2776.           itype = build_index_type (build_int_2 (has_virtual, 0));
  2777.           atype = build_array_type (vtable_entry_type, itype);
  2778.           layout_type (atype);
  2779.           vfield = copy_node (vfield);
  2780.           TREE_TYPE (vfield) = build_pointer_type (atype);
  2781. #if 0
  2782.           /* In the case of single inheritance, we can
  2783.          just move up the tree, since we share the
  2784.          same vptr slot.  */
  2785.           if (TREE_CHAIN (vfields) == NULL_TREE)
  2786.         vfields = CLASSTYPE_AS_LIST (t);
  2787. #endif
  2788.         }
  2789.     }
  2790.  
  2791.       CLASSTYPE_VFIELD (t) = vfield;
  2792.       if (TREE_TYPE (CLASS_ASSOC_VTABLE (t)) != atype)
  2793.     {
  2794.       TREE_TYPE (CLASS_ASSOC_VTABLE (t)) = atype;
  2795.       layout_decl (CLASS_ASSOC_VTABLE (t));
  2796.       DECL_ALIGN (CLASS_ASSOC_VTABLE (t))
  2797.         = MAX (TYPE_ALIGN (double_type_node),
  2798.            DECL_ALIGN (CLASS_ASSOC_VTABLE (t)));
  2799.     }
  2800.     }
  2801.   else if (first_vfn_base_index)
  2802.     CLASSTYPE_VFIELD (t) = vfield;
  2803.   CLASSTYPE_VFIELDS (t) = vfields;
  2804.  
  2805.   /* Set all appropriate CLASSTYPE_... flags for this type
  2806.      and its variants.  */
  2807.   TYPE_NEEDS_CONSTRUCTOR (t) |= needs_ctor || TYPE_HAS_CONSTRUCTOR (t);
  2808.   TYPE_NEEDS_CONSTRUCTING (t)
  2809.     |= ((TYPE_NEEDS_CONSTRUCTOR (t)|TYPE_USES_VIRTUAL_BASECLASSES (t))
  2810.     || (has_virtual | first_vfn_base_index)
  2811.     || any_default_members);
  2812.   TYPE_NEEDS_DESTRUCTOR (t) |= needs_dtor || TYPE_HAS_DESTRUCTOR (t);
  2813.   finish_struct_bits (t, first_vfn_base_index, max_has_virtual);
  2814.  
  2815.   /* Promote each bit-field's type to int if it is narrower than that.
  2816.      Also warn (or error) if static members are specified for a class
  2817.      which takes a constructor.  */
  2818.   for (x = fields; x; x = TREE_CHAIN (x))
  2819.     {
  2820.       if (TREE_PACKED (x)
  2821.       && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE
  2822.       && (TREE_INT_CST_LOW (DECL_SIZE (x)) * DECL_SIZE_UNIT (x)
  2823.           < TYPE_PRECISION (integer_type_node)))
  2824.     TREE_TYPE (x) = integer_type_node;
  2825.     }
  2826.  
  2827.   if (TYPE_HAS_CONSTRUCTOR (t))
  2828.     {
  2829.       tree vfields = CLASSTYPE_VFIELDS (t);
  2830.  
  2831.       while (vfields)
  2832.     {
  2833.       /* Mark the fact that constructor for T
  2834.          could affect anybody inheriting from T
  2835.          who wants to initialize vtables for VFIELDS's type.  */
  2836.       if (TREE_TYPE (vfields))
  2837.         TREE_ADDRESSABLE (vfields) = 1;
  2838.       vfields = TREE_CHAIN (vfields);
  2839.     }
  2840.       if (any_default_members != 0)
  2841.     build_class_init_list (t);
  2842.     }
  2843.   else if (TYPE_NEEDS_CONSTRUCTING (t))
  2844.     build_class_init_list (t);
  2845.  
  2846.   if (current_lang_name == lang_name_cplusplus)
  2847.     {
  2848.       if (! CLASSTYPE_DECLARED_EXCEPTION (t))
  2849.     embrace_waiting_friends (t);
  2850.  
  2851.       /* Write out inline function definitions.  */
  2852.       do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
  2853.       CLASSTYPE_INLINE_FRIENDS (t) = 0;
  2854.     }
  2855.  
  2856.   if (CLASSTYPE_VSIZE (t) != 0)
  2857.     {
  2858.       TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (CLASS_ASSOC_VTABLE (t)), vfield);
  2859.  
  2860.       if ((flag_this_is_variable & 1) == 0)
  2861.     {
  2862.       tree vtbl_ptr = build_decl (VAR_DECL, get_identifier (VPTR_NAME),
  2863.                       TREE_TYPE (vfield));
  2864.       TREE_REGDECL (vtbl_ptr) = 1;
  2865.       CLASSTYPE_VTBL_PTR (t) = vtbl_ptr;
  2866.     }
  2867.       if (DECL_FIELD_CONTEXT (vfield) != t)
  2868.     {
  2869.       tree assoc = assoc_value (DECL_FIELD_CONTEXT (vfield), t);
  2870.       tree offset = ASSOC_OFFSET (assoc);
  2871.  
  2872.       vfield = copy_node (vfield);
  2873.  
  2874.       if (! integer_zerop (offset))
  2875.         offset = convert_units (offset, BITS_PER_UNIT, 1);
  2876.       if (DECL_OFFSET (vfield))
  2877.         offset = genop (PLUS_EXPR, offset, build_int (DECL_OFFSET (vfield)));
  2878.       DECL_FIELD_CONTEXT (vfield) = t;
  2879.       DECL_OFFSET (vfield) = TREE_INT_CST_LOW (offset);
  2880.       CLASSTYPE_VFIELD (t) = vfield;
  2881.     }
  2882.     }
  2883.  
  2884.   /* Make the rtl for any new vtables we have created, and unmark
  2885.      the base types we marked.  */
  2886.   unmark_finished_struct (t);
  2887.  
  2888.   /* Now out of this class's scope.  However, if this class defined
  2889.      any new typedefs, then we must export those to the outer
  2890.      binding level.  This is unpleasant.  */
  2891.   x = gettags ();
  2892.  
  2893.   popclass (0);
  2894.  
  2895. #if 0
  2896.   /* Remove aggregate types from the list of tags,
  2897.      since these appear at global scope.  */
  2898.   while (x && IS_AGGR_TYPE (TREE_VALUE (x)))
  2899.     x = TREE_CHAIN (x);
  2900.   CLASSTYPE_TAGS (t) = x;
  2901.   y = x;
  2902.   while (x)
  2903.     {
  2904.       if (IS_AGGR_TYPE (TREE_VALUE (x)))
  2905.     TREE_CHAIN (y) = TREE_CHAIN (x);
  2906.       x = TREE_CHAIN (x);
  2907.     }
  2908. #endif
  2909.  
  2910.   hack_incomplete_structures (t);
  2911.  
  2912.   resume_momentary (old);
  2913.  
  2914.   return t;
  2915. }
  2916.  
  2917. /* Return non-zero if the effective type of INSTANCE is static.
  2918.    Used to determine whether the virtual function table is needed
  2919.    or not.  */
  2920. int
  2921. resolves_to_fixed_type_p (instance)
  2922.      tree instance;
  2923. {
  2924.   switch (TREE_CODE (instance))
  2925.     {
  2926.     case ADDR_EXPR:
  2927.       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0));
  2928.  
  2929.     case COMPONENT_REF:
  2930.       /* Don't let pointers to members look like they hold a fixed type.  */
  2931.       if (TREE_CODE (TREE_OPERAND (instance, 1)) != FIELD_DECL)
  2932.     return 0;
  2933.  
  2934.     case VAR_DECL:
  2935.     case PARM_DECL:
  2936.     case NEW_EXPR:
  2937.       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
  2938.     return 1;
  2939.  
  2940.     default:
  2941.       return 0;
  2942.     }
  2943. }
  2944.  
  2945. /* Ordering function for overload resolution.  */
  2946. int
  2947. rank_for_overload (x, y)
  2948.      struct candidate *x, *y;
  2949. {
  2950.   if (y->evil - x->evil)
  2951.     return y->evil - x->evil;
  2952.   if ((y->harshness[0] & 128) ^ (x->harshness[0] & 128))
  2953.     return y->harshness[0] - x->harshness[0];
  2954.   if (y->user - x->user)
  2955.     return y->user - x->user;
  2956.   if (y->b_or_d - x->b_or_d)
  2957.     return y->b_or_d - x->b_or_d;
  2958.   return y->easy - x->easy;
  2959. }
  2960.  
  2961. /* TYPE is the type we wish to convert to.  PARM is the parameter
  2962.    we have to work with.  We use a somewhat arbitrary cost function
  2963.    to measure this conversion.  */
  2964. static int
  2965. convert_harshness (type, parmtype, parm)
  2966.      register tree type, parmtype;
  2967.      tree parm;
  2968. {
  2969.   register enum tree_code codel = TREE_CODE (type);
  2970.   register enum tree_code coder = TREE_CODE (parmtype);
  2971.  
  2972. #ifdef GATHER_STATISTICS
  2973.   n_convert_harshness++;
  2974. #endif
  2975.  
  2976.   if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
  2977.     return 0;
  2978.  
  2979.   if (coder == ERROR_MARK)
  2980.     return 1;
  2981.  
  2982.   if (codel == POINTER_TYPE
  2983.       && (coder == METHOD_TYPE || coder == FUNCTION_TYPE))
  2984.     {
  2985.       tree p1, p2;
  2986.       int harshness, new_harshness;
  2987.  
  2988.       /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be.  */
  2989.       type = TREE_TYPE (type);
  2990.  
  2991.       if (coder != TREE_CODE (type))
  2992.     return 1;
  2993.  
  2994.       harshness = 0;
  2995.  
  2996.       /* We allow the default conversion between function type
  2997.      and pointer-to-function type for free.  */
  2998.       if (type == parmtype)
  2999.     return 0;
  3000.  
  3001.       /* Compare return types.  */
  3002.       harshness |= convert_harshness (TREE_TYPE (type), TREE_TYPE (parmtype), 0);
  3003.       if (harshness & 1)
  3004.     return 1;
  3005.       p1 = TYPE_ARG_TYPES (type);
  3006.       p2 = TYPE_ARG_TYPES (parmtype);
  3007.       while (p1 && p2)
  3008.     {
  3009.       new_harshness = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2), 0);
  3010.       if (new_harshness & 1)
  3011.         return 1;
  3012.       if ((new_harshness & 7) == 0)
  3013.         harshness += new_harshness;
  3014.       else
  3015.         harshness |= new_harshness;
  3016.       p1 = TREE_CHAIN (p1);
  3017.       p2 = TREE_CHAIN (p2);
  3018.     }
  3019.       if (p1 == p2)
  3020.     return harshness;
  3021.       if (p2)
  3022.     return 1;
  3023.       if (p1)
  3024.     return harshness | (TREE_PURPOSE (p1) == NULL_TREE);
  3025.     }
  3026.   else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
  3027.     {
  3028.       int harshness;
  3029.  
  3030.       /* Get to the OFFSET_TYPE that this might be.  */
  3031.       type = TREE_TYPE (type);
  3032.  
  3033.       if (coder != TREE_CODE (type))
  3034.     return 1;
  3035.  
  3036.       harshness = 0;
  3037.  
  3038.       if (TYPE_OFFSET_BASETYPE (type) == TYPE_OFFSET_BASETYPE (parmtype))
  3039.     harshness = 0;
  3040.       else if (get_base_type (TYPE_OFFSET_BASETYPE (type),
  3041.                   TYPE_OFFSET_BASETYPE (parmtype), 0))
  3042.     harshness = (1<<3);
  3043.       else
  3044.     return 1;
  3045.       /* Now test the OFFSET_TYPE's target compatability.  */
  3046.       type = TREE_TYPE (type);
  3047.       parmtype = TREE_TYPE (parmtype);
  3048.     }
  3049.  
  3050.   if (coder == UNKNOWN_TYPE)
  3051.     {
  3052.       if (codel == FUNCTION_TYPE
  3053.       || codel == METHOD_TYPE
  3054.       || (codel == POINTER_TYPE
  3055.           && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
  3056.           || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
  3057.     return 0;
  3058.       return 1;
  3059.     }
  3060.  
  3061.   if (coder == VOID_TYPE)
  3062.     return 1;
  3063.  
  3064.   if (codel == ENUMERAL_TYPE || codel == INTEGER_TYPE)
  3065.     {
  3066.       /* Control equivalence of ints an enums.  */
  3067.  
  3068.       if (codel == ENUMERAL_TYPE
  3069.       && flag_int_enum_equivalence == 0)
  3070.     {
  3071.       /* Enums can be converted to ints, but not vice-versa.  */
  3072.       if (coder != ENUMERAL_TYPE
  3073.           || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
  3074.         return 1;
  3075.     }
  3076.  
  3077.       /* else enums and ints (almost) freely interconvert.  */
  3078.  
  3079.       if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
  3080.     {
  3081.       int easy = TREE_UNSIGNED (type) ^ TREE_UNSIGNED (parmtype);
  3082.       if (codel != coder)
  3083.         easy += 1;
  3084.       if (TYPE_MODE (type) != TYPE_MODE (parmtype))
  3085.         easy += 2;
  3086.       return (easy << 4);
  3087.     }
  3088.       else if (coder == REAL_TYPE)
  3089.     return (4<<4);
  3090.     }
  3091.  
  3092.   if (codel == REAL_TYPE)
  3093.     if (coder == REAL_TYPE)
  3094.       /* Shun converting between float and double if a choice exists.  */
  3095.       {
  3096.     if (TYPE_MODE (type) != TYPE_MODE (parmtype))
  3097.       return (2<<4);
  3098.     return 0;
  3099.       }
  3100.     else if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
  3101.       return (4<<4);
  3102.  
  3103.   /* convert arrays which have not previously been converted.  */
  3104.   if (codel == ARRAY_TYPE)
  3105.     codel = POINTER_TYPE;
  3106.   if (coder == ARRAY_TYPE)
  3107.     coder = POINTER_TYPE;
  3108.  
  3109.   /* Conversions among pointers */
  3110.   if (codel == POINTER_TYPE && coder == POINTER_TYPE)
  3111.     {
  3112.       register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  3113.       register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
  3114.       int penalty = 4 * (ttl != ttr);
  3115.       /* Anything converts to void *.  void * converts to anything.
  3116.      Since these may be `const void *' (etc.) use VOID_TYPE
  3117.      instead of void_type_node.
  3118.      Otherwise, the targets must be the same,
  3119.      except that we do allow (at some cost) conversion
  3120.      between signed and unsinged pointer types.  */
  3121.  
  3122.       if ((TREE_CODE (ttl) == METHOD_TYPE
  3123.        || TREE_CODE (ttl) == FUNCTION_TYPE)
  3124.       && TREE_CODE (ttl) == TREE_CODE (ttr))
  3125.     {
  3126.       if (comptypes (ttl, ttr, -1))
  3127.         return penalty<<4;
  3128.       return 1;
  3129.     }
  3130.  
  3131.       if (!(TREE_CODE (ttl) == VOID_TYPE
  3132.         || TREE_CODE (ttr) == VOID_TYPE
  3133.         || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
  3134.         && (ttl = unsigned_type (ttl),
  3135.             ttr = unsigned_type (ttr),
  3136.             penalty = 10, 0))
  3137.         || (comp_target_types (ttl, ttr, 0))))
  3138.     return 1;
  3139.  
  3140.       if (ttr == ttl)
  3141.     return 4;
  3142.  
  3143.       if (IS_AGGR_TYPE (ttl) && IS_AGGR_TYPE (ttr))
  3144.     {
  3145.       int b_or_d = get_base_distance (ttl, ttr, 0, 0);
  3146.       if (b_or_d < 0)
  3147.         return 1;
  3148.       return (b_or_d<<3) | 4;
  3149.     }
  3150.  
  3151.       return (penalty<<4);
  3152.     }
  3153.  
  3154.   if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
  3155.     {
  3156.       /* This is not a bad match, but don't let it beat
  3157.      integer-enum combinations.  */
  3158.       if (parm && integer_zerop (parm))
  3159.     return (4<<4);
  3160.     }
  3161.  
  3162.   /* C++: one of the types must be a reference type.  */
  3163.   {
  3164.     tree ttl, ttr;
  3165.     register tree intype = TYPE_MAIN_VARIANT (parmtype);
  3166.     register enum tree_code form = TREE_CODE (intype);
  3167.     int penalty;
  3168.  
  3169.     if (codel == REFERENCE_TYPE || coder == REFERENCE_TYPE)
  3170.       {
  3171.     ttl = TYPE_MAIN_VARIANT (type);
  3172.  
  3173.     if (codel == REFERENCE_TYPE)
  3174.       {
  3175.         ttl = TYPE_MAIN_VARIANT (TREE_TYPE (ttl));
  3176.  
  3177.         if (form == OFFSET_TYPE)
  3178.           {
  3179.         intype = TREE_TYPE (intype);
  3180.         form = TREE_CODE (intype);
  3181.           }
  3182.  
  3183.         if (form == REFERENCE_TYPE)
  3184.           {
  3185.         intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
  3186.  
  3187.         if (ttl == intype)
  3188.           return 0;
  3189.         penalty = 2;
  3190.           }
  3191.         else
  3192.           {
  3193.         /* Can reference be built up?  */
  3194.         if (ttl == intype)
  3195.           {
  3196.             return 0;
  3197.           }
  3198.         else
  3199.           penalty = 2;
  3200.           }
  3201.       }
  3202.     else if (form == REFERENCE_TYPE)
  3203.       {
  3204.         if (parm)
  3205.           {
  3206.         tree tmp = convert_from_reference (parm);
  3207.         intype = TYPE_MAIN_VARIANT (TREE_TYPE (tmp));
  3208.           }
  3209.         else
  3210.           {
  3211.         intype = parmtype;
  3212.         do
  3213.           {
  3214.             intype = TREE_TYPE (intype);
  3215.           }
  3216.         while (TREE_CODE (intype) == REFERENCE_TYPE);
  3217.         intype = TYPE_MAIN_VARIANT (intype);
  3218.           }
  3219.  
  3220.         if (ttl == intype)
  3221.           return 0;
  3222.         else
  3223.           penalty = 2;
  3224.       }
  3225.  
  3226.     if (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (intype))
  3227.       {
  3228.         ttl = unsigned_type (ttl);
  3229.         intype = unsigned_type (intype);
  3230.         penalty += 2;
  3231.       }
  3232.  
  3233.     ttr = intype;
  3234.  
  3235.     /* If the initializer is not an lvalue, then it does not
  3236.        matter if we make life easier for the programmer
  3237.        by creating a temporary variable with which to
  3238.        hold the result.  */
  3239.     if (parm && (coder == INTEGER_TYPE
  3240.              || coder == ENUMERAL_TYPE
  3241.              || coder == REAL_TYPE)
  3242.         && ! lvalue_p (parm))
  3243.       return (convert_harshness (ttl, ttr, 0) | (penalty << 4));
  3244.  
  3245.     if (ttl == ttr)
  3246.       return 4;
  3247.  
  3248.     /* Pointers to voids always convert for pointers.  But
  3249.        make them less natural than more specific matches.  */
  3250.     if (TREE_CODE (ttl) == POINTER_TYPE && TREE_CODE (ttr) == POINTER_TYPE)
  3251.       if (TREE_TYPE (ttl) == void_type_node
  3252.           || TREE_TYPE (ttr) == void_type_node)
  3253.         return ((penalty+1)<<4);
  3254.  
  3255.     if (parm && codel != REFERENCE_TYPE)
  3256.       return (convert_harshness (ttl, ttr, 0) | (penalty << 4));
  3257.  
  3258.     /* Here it does matter.  If this conversion is from
  3259.        derived to base, allow it.  Otherwise, types must
  3260.        be compatible in the strong sense.  */
  3261.     if (IS_AGGR_TYPE (ttl) && IS_AGGR_TYPE (ttr))
  3262.       {
  3263.         int b_or_d = get_base_distance (ttl, ttr, 0, 0);
  3264.         if (b_or_d < 0)
  3265.           return 1;
  3266. #if AMBIGUOUS_WORKING
  3267.         if (ttl == TYPE_MAIN_VARIANT (type)
  3268.         && TYPE_GETS_INIT_REF (type))
  3269.           return (b_or_d<<3) | 6;
  3270. #endif
  3271.         return (b_or_d<<3) | 4;
  3272.       }
  3273.  
  3274.     if (comp_target_types (ttl, intype, 1))
  3275.       return (penalty<<4);
  3276.       }
  3277.   }
  3278.   if (codel == RECORD_TYPE && coder == RECORD_TYPE)
  3279.     {
  3280.       int b_or_d = get_base_distance (type, parmtype, 0, 0);
  3281.       if (b_or_d < 0)
  3282.     return 1;
  3283. #if AMBIGUOUS_WORKING
  3284.       if (TYPE_GETS_INIT_REF (type))
  3285.     return (b_or_d<<3) | 6;
  3286. #endif
  3287.       return (b_or_d<<3) | 4;
  3288.     }
  3289.   return 1;
  3290. }
  3291.  
  3292. /* Algorithm: Start out with no stikes against.  For each argument
  3293.    which requires a (subjective) hard conversion (such as between
  3294.    floating point and integer), issue a strike.  If there are the same
  3295.    number of formal and actual parameters in the list, there will be at
  3296.    least on strike, otherwise an exact match would have been found.  If
  3297.    there are not the same number of arguments in the type lists, we are
  3298.    not dead yet: a `...' means that we can have more parms then were
  3299.    declared, and if we wind up in the default argument section of the
  3300.    list those can be used as well.  If an exact match could be found for
  3301.    one of those cases, return it immediately.  Otherwise, Rank the fields
  3302.    so that fields with fewer strikes are tried first.
  3303.  
  3304.    Conversions between builtin and user-defined types are allowed, but
  3305.    no function involving such a conversion is prefered to one which
  3306.    does not require such a conversion.  Furthermore, such conversions
  3307.    must be unique.  */
  3308.  
  3309. void
  3310. compute_conversion_costs (function, tta_in, cp, arglen)
  3311.      tree function;
  3312.      tree tta_in;
  3313.      struct candidate *cp;
  3314.      int arglen;
  3315. {
  3316.   tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
  3317.   tree ttf = ttf_in;
  3318.   tree tta = tta_in;
  3319.  
  3320.   /* Start out with no strikes against.  */
  3321.   int evil_strikes = 0;
  3322.   int user_strikes = 0;
  3323.   int b_or_d_strikes = 0;
  3324.   int easy_strikes = 0;
  3325.  
  3326.   int strike_index = 0, win, lose;
  3327.  
  3328. #ifdef GATHER_STATISTICS
  3329.   n_compute_conversion_costs++;
  3330. #endif
  3331.  
  3332.   cp->function = function;
  3333.   cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
  3334.   cp->u.bad_arg = 0;        /* optimistic!  */
  3335.  
  3336.   bzero (cp->harshness, (arglen+1) * sizeof (short));
  3337.  
  3338.   while (ttf && tta)
  3339.     {
  3340.       int harshness;
  3341.  
  3342.       if (ttf == void_list_node)
  3343.     break;
  3344.  
  3345.       if (type_unknown_p (TREE_VALUE (tta)))
  3346.     {      
  3347.       /* Must perform some instantiation here.  */
  3348.       tree rhs = TREE_VALUE (tta);
  3349.       tree lhstype = TREE_VALUE (ttf);
  3350.  
  3351.       /* @@ This is to undo what `grokdeclarator' does to
  3352.          parameter types.  It really should go through
  3353.          something more general.  */
  3354.  
  3355.       TREE_TYPE (tta) = unknown_type_node;
  3356.       if (TREE_CODE (rhs) == OP_IDENTIFIER)
  3357.         rhs = build_instantiated_decl (lhstype, rhs);
  3358.       else
  3359.         {
  3360.           /* Keep quiet about possible contravariance violations.  */
  3361.           extern int inhibit_warnings;
  3362.           int old_inhibit_warnings = inhibit_warnings;
  3363.           inhibit_warnings = 1;
  3364.  
  3365.           rhs = instantiate_type (lhstype, rhs, 0);
  3366.  
  3367.           inhibit_warnings = old_inhibit_warnings;
  3368.         }
  3369.  
  3370.       if (TREE_CODE (rhs) == ERROR_MARK)
  3371.         harshness = 1;
  3372.       else
  3373.         {
  3374.           harshness = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
  3375.           /* harshness |= 2; */
  3376.         }
  3377.     }
  3378.       else
  3379.     harshness = convert_harshness (TREE_VALUE (ttf), TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta));
  3380.  
  3381.       cp->harshness[strike_index] = harshness;
  3382.       if (harshness & 1)
  3383.     {
  3384.       cp->u.bad_arg = strike_index;
  3385.       evil_strikes = 1;
  3386.     }
  3387.       else if (harshness & 2)
  3388.     {
  3389.       user_strikes += 1;
  3390.     }
  3391.       else if (harshness & 4)
  3392.     {
  3393.       b_or_d_strikes += (harshness >> 3);
  3394.     }
  3395.       else
  3396.     easy_strikes += harshness >> 4;
  3397.       ttf = TREE_CHAIN (ttf);
  3398.       tta = TREE_CHAIN (tta);
  3399.       strike_index += 1;
  3400.     }
  3401.  
  3402.   if (tta)
  3403.     {
  3404.       /* ran out of formals, and parmlist is fixed size.  */
  3405.       if (ttf /* == void_type_node */)
  3406.     {
  3407.       cp->evil = 1;
  3408.       cp->u.bad_arg = -1;
  3409.       return;
  3410.     }
  3411.     }
  3412.   else if (ttf && ttf != void_list_node)
  3413.     {
  3414.       /* ran out of actuals, and no defaults.  */
  3415.       if (TREE_PURPOSE (ttf) == NULL_TREE)
  3416.     {
  3417.       cp->evil = 1;
  3418.       cp->u.bad_arg = -2;
  3419.       return;
  3420.     }
  3421.       /* Store index of first default.  */
  3422.       cp->harshness[arglen] = strike_index+1;
  3423.     }
  3424.   else cp->harshness[arglen] = 0;
  3425.  
  3426.   /* Argument list lengths work out, so don't need to check them again.  */
  3427.   if (evil_strikes)
  3428.     {
  3429.       /* We do not check for derived->base conversions here, since in
  3430.      no case would they give evil strike counts, unless such conversions
  3431.      are somehow ambiguous.  */
  3432.  
  3433.       /* See if any user-defined conversions apply.
  3434.          But make sure that we do not loop.  */
  3435.       static int dont_convert_types = 0;
  3436.  
  3437.       if (dont_convert_types)
  3438.     {
  3439.       cp->evil = 1;
  3440.       return;
  3441.     }
  3442.  
  3443.       win = 0;            /* Only get one chance to win.  */
  3444.       ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
  3445.       tta = tta_in;
  3446.       strike_index = 0;
  3447.       evil_strikes = 0;
  3448.  
  3449.       while (ttf && tta)
  3450.     {
  3451.       if (ttf == void_list_node)
  3452.         break;
  3453.  
  3454.       lose = cp->harshness[strike_index];
  3455.       if (lose&1)
  3456.         {
  3457.           tree actual_type = TREE_TYPE (TREE_VALUE (tta));
  3458.           tree formal_type = TREE_VALUE (ttf);
  3459.  
  3460.           dont_convert_types = 1;
  3461.  
  3462.           if (TREE_CODE (formal_type) == REFERENCE_TYPE)
  3463.         formal_type = TREE_TYPE (formal_type);
  3464.           if (TREE_CODE (actual_type) == REFERENCE_TYPE)
  3465.         actual_type = TREE_TYPE (actual_type);
  3466.  
  3467.           if (formal_type != error_mark_node
  3468.           && actual_type != error_mark_node)
  3469.         {
  3470.           formal_type = TYPE_MAIN_VARIANT (formal_type);
  3471.           actual_type = TYPE_MAIN_VARIANT (actual_type);
  3472.  
  3473.           if (TYPE_HAS_CONSTRUCTOR (formal_type))
  3474.             {
  3475.               /* If it has a constructor for this type, try to use it.  */
  3476.               if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
  3477.               != error_mark_node)
  3478.             {
  3479.               /* @@ There is no way to save this result yet.
  3480.                  @@ So success is NULL_TREE for now.  */
  3481.               win++;
  3482.             }
  3483.             }
  3484.           if (TYPE_LANG_SPECIFIC (actual_type) && TYPE_HAS_CONVERSION (actual_type))
  3485.             {
  3486.               if (TREE_CODE (formal_type) == INTEGER_TYPE
  3487.               && TYPE_HAS_INT_CONVERSION (actual_type))
  3488.             win++;
  3489.               else if (TREE_CODE (formal_type) == REAL_TYPE
  3490.                    && TYPE_HAS_REAL_CONVERSION (actual_type))
  3491.             win++;
  3492.               else
  3493.             {
  3494.               tree conv = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_VALUE (tta), 0);
  3495.               if (conv)
  3496.                 {
  3497.                   if (conv == error_mark_node)
  3498.                 win += 2;
  3499.                   else
  3500.                 win++;
  3501.                 }
  3502.               else if (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE)
  3503.                 {
  3504.                   conv = build_type_conversion (CALL_EXPR, formal_type, TREE_VALUE (tta), 0);
  3505.                   if (conv)
  3506.                 {
  3507.                   if (conv == error_mark_node)
  3508.                     win += 2;
  3509.                   else
  3510.                     win++;
  3511.                 }
  3512.                 }
  3513.             }
  3514.             }
  3515.         }
  3516.           dont_convert_types = 0;
  3517.  
  3518.           if (win == 1)
  3519.         {
  3520.           user_strikes += 1;
  3521.           cp->harshness[strike_index] = 2;
  3522.           win = 0;
  3523.         }
  3524.           else
  3525.         {
  3526.           if (cp->u.bad_arg > strike_index)
  3527.             cp->u.bad_arg = strike_index;
  3528.  
  3529.           evil_strikes = win ? 2 : 1;
  3530.           break;
  3531.         }
  3532.         }
  3533.  
  3534.       ttf = TREE_CHAIN (ttf);
  3535.       tta = TREE_CHAIN (tta);
  3536.       strike_index += 1;
  3537.     }
  3538.     }
  3539.  
  3540.   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
  3541.     {
  3542.       /* Const member functions get a small penalty because defaulting
  3543.      to const is less useful than defaulting to non-const. */
  3544.       if (TREE_READONLY (TREE_TYPE (TREE_VALUE (ttf_in))))
  3545.     {
  3546.       cp->harshness[0] += 16;
  3547.       ++easy_strikes;
  3548.     }
  3549.       else
  3550.     {
  3551.       /* Calling a non-const member function from a const member function
  3552.          is probably invalid, but for now we let it only draw a warning.
  3553.          We indicate that such a mismatch has occured by setting the
  3554.          harshness to a maximum value.  */
  3555.       if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
  3556.           && (TREE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
  3557.         cp->harshness[0] |= 128;
  3558.     }
  3559.     }
  3560.  
  3561.   cp->evil = evil_strikes;
  3562.   cp->user = user_strikes;
  3563.   cp->b_or_d = b_or_d_strikes;
  3564.   cp->easy = easy_strikes;
  3565. }
  3566.  
  3567. struct candidate *
  3568. ideal_candidate (basetype, candidates, n_candidates, parms, len)
  3569.      tree basetype;
  3570.      struct candidate *candidates;
  3571.      int n_candidates;
  3572.      tree parms;
  3573.      int len;
  3574. {
  3575.   struct candidate *cp = candidates + n_candidates;
  3576.   int index, i;
  3577.   tree ttf;
  3578.  
  3579.   qsort (candidates,        /* char *base */
  3580.      n_candidates,        /* int nel */
  3581.      sizeof (struct candidate), /* int width */
  3582.      rank_for_overload);    /* int (*compar)() */
  3583.  
  3584.   /* If the best candidate requires user-defined conversions,
  3585.      and its user-defined conversions are a strict subset
  3586.      of all other candidates requiring user-defined conversions,
  3587.      then it is, in fact, the best.  */
  3588.   for (i = -1; cp + i != candidates; i--)
  3589.     if (cp[i].user == 0)
  3590.       break;
  3591.  
  3592.   if (i < -1)
  3593.     {
  3594.       tree ttf0;
  3595.  
  3596.       /* Check that every other candidate requires those conversions
  3597.      as a strict subset of their conversions.  */
  3598.       if (cp[i].user == cp[-1].user)
  3599.     goto non_subset;
  3600.  
  3601.       /* Look at subset relationship more closely.  */
  3602.       while (i != -1)
  3603.     {
  3604.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)),
  3605.            ttf0 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function)),
  3606.            index = 0;
  3607.            index < len;
  3608.            ttf = TREE_CHAIN (ttf), ttf0 = TREE_CHAIN (ttf0), index++)
  3609.         if (cp[i].harshness[index] & 2)
  3610.           {
  3611.         /* If our "best" candidate also needs a conversion,
  3612.            it must be the same one.  */
  3613.         if ((cp[-1].harshness[index] & 2)
  3614.             && TREE_VALUE (ttf) != TREE_VALUE (ttf0))
  3615.           goto non_subset;
  3616.           }
  3617.       i++;
  3618.     }
  3619.       /* The best was the best.  */
  3620.       return cp - 1;
  3621.     non_subset:
  3622.       /* Use other rules for determining "bestness".  */
  3623.       ;
  3624.     }
  3625.  
  3626.   /* If the best two candidates we find require user-defined
  3627.      conversions, we may need to report and error message.  */
  3628.   if (cp[-1].user && cp[-2].user
  3629.       && (cp[-1].b_or_d || cp[-2].b_or_d == 0))
  3630.     {
  3631.       /* If the best two methods found involved user-defined
  3632.      type conversions, then we must see whether one
  3633.      of them is exactly what we wanted.  If not, then
  3634.      we have an ambiguity.  */
  3635.       int best = 0;
  3636.       tree tta = parms;
  3637.       tree f1, p1;
  3638.  
  3639. #if AMBIGUOUS_WORKING
  3640.       if (cp[-1].b_or_d == 0
  3641.       && cp[-1].easy == 0
  3642.       && (cp[-2].b_or_d | cp[-2].easy) > 0)
  3643.     return cp - 1;
  3644. #endif
  3645.  
  3646.       /* Stash all of our parameters in safe places
  3647.      so that we can perform type conversions in place.  */
  3648.       while (tta)
  3649.     {
  3650.       TREE_PURPOSE (tta) = TREE_VALUE (tta);
  3651.       tta = TREE_CHAIN (tta);
  3652.     }
  3653.  
  3654.       i = 0;
  3655.       do
  3656.     {
  3657.       int exact_conversions = 0;
  3658.  
  3659.       i -= 1;
  3660.       tta = parms;
  3661.       if (DECL_STATIC_FUNCTION_P (cp[i].function))
  3662.         tta = TREE_CHAIN (tta);
  3663.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)), index = 0;
  3664.            index < len;
  3665.            tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
  3666.         {
  3667.           if (cp[i].harshness[index] & 2)
  3668.         {
  3669.           TREE_VALUE (tta)
  3670.             = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_PURPOSE (tta), 2);
  3671.           if (TREE_VALUE (tta))
  3672.             {
  3673.               if (TREE_CODE (TREE_VALUE (tta)) != CONVERT_EXPR
  3674.               && (TREE_CODE (TREE_VALUE (tta)) != NOP_EXPR
  3675.                   || comp_target_types (TREE_TYPE (TREE_VALUE (tta)),
  3676.                             TREE_TYPE (TREE_OPERAND (TREE_VALUE (tta), 0)), 1)))
  3677.             exact_conversions += 1;
  3678.             }
  3679.           else if (IS_AGGR_TYPE (TREE_VALUE (ttf))
  3680.                || (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE
  3681.                    && IS_AGGR_TYPE (TREE_TYPE (TREE_VALUE (ttf)))))
  3682.             {
  3683.               /* To get here we had to have succeeded via
  3684.              a constructor.  */
  3685.               TREE_VALUE (tta) = TREE_PURPOSE (tta);
  3686.               exact_conversions += 1;
  3687.             }
  3688.         }
  3689.         }
  3690.       if (exact_conversions == cp[i].user)
  3691.         {
  3692.           if (best == 0)
  3693.         {
  3694.           best = i;
  3695.           f1 = cp[best].function;
  3696.           p1 = TYPE_ARG_TYPES (TREE_TYPE (f1));
  3697.         }
  3698.           else
  3699.         {
  3700.           /* Don't complain if next best is from base class.  */
  3701.           tree f2 = cp[i].function;
  3702.           tree p2 = TYPE_ARG_TYPES (TREE_TYPE (f2));
  3703.  
  3704.           if (TREE_CODE (TREE_TYPE (f1)) == METHOD_TYPE
  3705.               && TREE_CODE (TREE_TYPE (f2)) == METHOD_TYPE
  3706.               && (cp[i].harshness[0] & 4) != 0
  3707.               && cp[best].harshness[0] < cp[i].harshness[0])
  3708.             {
  3709. #if 0
  3710.               /* For LUCID.  */
  3711.               if (! compparms (TREE_CHAIN (p1), TREE_CHAIN (p2), 1))
  3712.             goto ret0;
  3713.               else
  3714. #endif
  3715.             continue;
  3716.             }
  3717.           else goto ret0;
  3718.         }
  3719.         }
  3720.     } while (cp + i != candidates);
  3721.  
  3722.       if (best)
  3723.     {
  3724.       int exact_conversions = cp[best].user;
  3725.       tta = parms;
  3726.       if (DECL_STATIC_FUNCTION_P (cp[best].function))
  3727.         tta = TREE_CHAIN (parms);
  3728.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[best].function)), index = 0;
  3729.            exact_conversions > 0;
  3730.            tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
  3731.         {
  3732.           if (cp[best].harshness[index] & 2)
  3733.         {
  3734.           /* We must now fill in the slot we left behind.
  3735.              @@ This could be optimized to use the value previously
  3736.              @@ computed by build_type_conversion in some cases.  */
  3737.           TREE_VALUE (tta) = convert (TREE_VALUE (ttf), TREE_PURPOSE (tta));
  3738.           exact_conversions -= 1;
  3739.         }
  3740.           else TREE_VALUE (tta) = TREE_PURPOSE (tta);
  3741.         }
  3742.       return cp + best;
  3743.     }
  3744.       goto ret0;
  3745.     }
  3746.   /* If the best two candidates we find both use default parameters,
  3747.      we may need to report and error.  Don't need to worry if next-best
  3748.      candidate is forced to use user-defined conversion when best is not.  */
  3749.   if (cp[-2].user == 0
  3750.       && cp[-1].harshness[len] != 0 && cp[-2].harshness[len] != 0)
  3751.     {
  3752.       tree tt1 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function));
  3753.       tree tt2 = TYPE_ARG_TYPES (TREE_TYPE (cp[-2].function));
  3754.       int i = cp[-1].harshness[len];
  3755.       if (cp[-2].harshness[len] < i)
  3756.     i = cp[-2].harshness[len];
  3757.       while (--i > 0)
  3758.     {
  3759.       if (TYPE_MAIN_VARIANT (TREE_VALUE (tt1))
  3760.           != TYPE_MAIN_VARIANT (TREE_VALUE (tt2)))
  3761.         /* These lists are not identical, so we can choose our best candidate.  */
  3762.         return cp - 1;
  3763.       tt1 = TREE_CHAIN (tt1);
  3764.       tt2 = TREE_CHAIN (tt2);
  3765.     }
  3766.       /* To get here, both lists had the same parameters up to the defaults
  3767.      which were used.  This is an ambiguous request.  */
  3768.       goto ret0;
  3769.     }
  3770.  
  3771.   /* Otherwise, return our best candidate.  Note that if we get candidates
  3772.      from independent base classes, we have an ambiguity, even if one
  3773.      argument list look a little better than another one.  */
  3774.   if (cp[-1].b_or_d && basetype && TYPE_USES_MULTIPLE_INHERITANCE (basetype))
  3775.     {
  3776.       int i = n_candidates - 1, best;
  3777.       tree base1 = NULL_TREE;
  3778.  
  3779.       if (TREE_CODE (TREE_TYPE (candidates[i].function)) == FUNCTION_TYPE)
  3780.     return cp - 1;
  3781.  
  3782.       for (; i >= 0 && candidates[i].user == 0 && candidates[i].evil == 0; i--)
  3783.     {
  3784.       if (TREE_CODE (TREE_TYPE (candidates[i].function)) == METHOD_TYPE)
  3785.         {
  3786.           tree newbase = TYPE_METHOD_BASETYPE (TREE_TYPE (candidates[i].function));
  3787.  
  3788.           if (base1 != NULL_TREE)
  3789.         {
  3790.           if (newbase != base1
  3791.               && ! get_base_type (newbase, base1, 0))
  3792.             {
  3793.               char *buf = (char *)alloca (8192);
  3794.               error ("ambiguous request for function from distinct base classes of type `%s'", TYPE_NAME_STRING (basetype));
  3795.               error ("first candidate is `%s'", fndecl_as_string (buf, 0, candidates[best].function, 1));
  3796.               error ("second candidates is `%s'", fndecl_as_string (buf, 0, candidates[i].function, 1));
  3797.               return cp - 1;
  3798.             }
  3799.         }
  3800.           else
  3801.         {
  3802.           best = i;
  3803.           base1 = newbase;
  3804.         }
  3805.         }
  3806.       else return cp - 1;
  3807.     }
  3808.     }
  3809.  
  3810. #if AMBIGUOUS_WORKING
  3811.   if (cp[-1].user == cp[-2].user
  3812.       && cp[-1].b_or_d == cp[-2].b_or_d
  3813.       && cp[-1].easy == cp[-2].easy)
  3814.     goto ret0;
  3815. #endif
  3816.  
  3817.   return cp - 1;
  3818.  
  3819.  ret0:
  3820.   /* In the case where there is no ideal candidate, restore
  3821.      TREE_VALUE slots of PARMS from TREE_PURPOSE slots.  */
  3822.   while (parms)
  3823.     {
  3824.       TREE_VALUE (parms) = TREE_PURPOSE (parms);
  3825.       parms = TREE_CHAIN (parms);
  3826.     }
  3827.   return 0;
  3828. }
  3829.  
  3830. /* Assume that if the class referred to is not in the
  3831.    current class hierarchy, that it may be remote.
  3832.    PARENT is assumed to be of aggregate type here.  */
  3833. static int
  3834. may_be_remote (parent)
  3835.      tree parent;
  3836. {
  3837.   if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
  3838.     return 0;
  3839.  
  3840.   if (current_class_type == NULL_TREE)
  3841.     return 0;
  3842.   if (parent == current_class_type)
  3843.     return 0;
  3844.  
  3845.   if (get_base_type (parent, current_class_type, 0))
  3846.     return 0;
  3847.   return 1;
  3848. }
  3849.  
  3850. /* Return the number of bytes that the arglist in PARMS would
  3851.    occupy on the stack.  */
  3852. int
  3853. get_arglist_len_in_bytes (parms)
  3854.      tree parms;
  3855. {
  3856.   register tree parm;
  3857.   register int bytecount = 0;
  3858.  
  3859.   for (parm = parms; parm; parm = TREE_CHAIN (parm))
  3860.     {
  3861.       register tree pval = TREE_VALUE (parm);
  3862.       register int used, size;
  3863.  
  3864.       if (TREE_CODE (pval) == ERROR_MARK)
  3865.     continue;
  3866.       else if (TYPE_MODE (TREE_TYPE (pval)) != BLKmode)
  3867.     {
  3868.       used = size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (pval)));
  3869. #ifdef PUSH_ROUNDING
  3870.       size = PUSH_ROUNDING (size);
  3871. #endif
  3872.       used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
  3873.            / (PARM_BOUNDARY / BITS_PER_UNIT))
  3874.           * (PARM_BOUNDARY / BITS_PER_UNIT));
  3875.     }
  3876.       else
  3877.     {
  3878.       register tree size = size_in_bytes (TREE_TYPE (pval));
  3879.       register tree used_t = convert_units (convert_units (size, BITS_PER_UNIT, PARM_BOUNDARY),
  3880.                         PARM_BOUNDARY, BITS_PER_UNIT);
  3881.       used = TREE_INT_CST_LOW (used_t);
  3882.     }
  3883.       bytecount += used;
  3884.     }
  3885.   return bytecount;
  3886. }
  3887.  
  3888. tree
  3889. build_vfield_ref (datum, type)
  3890.      tree datum, type;
  3891. {
  3892.   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
  3893.     datum = convert_from_reference (datum);
  3894.  
  3895.   if (! TYPE_USES_VIRTUAL_BASECLASSES (type))
  3896.     return build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
  3897.           datum, CLASSTYPE_VFIELD (type));
  3898.   return build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), 0, 0);
  3899. }
  3900.  
  3901. /* Build a call to a member of an object.  I.e., one that overloads
  3902.    operator ()(), or is a pointer-to-function or pointer-to-method.  */
  3903. static tree
  3904. build_field_call (basetype_path, instance_ptr, name, parms, err_name)
  3905.      tree basetype_path;
  3906.      tree instance_ptr, name, parms;
  3907.      char *err_name;
  3908. {
  3909.   tree field, instance;
  3910.  
  3911.   if (instance_ptr == current_class_decl)
  3912.     {
  3913.       /* Check to see if we really have a reference to an instance variable
  3914.      with `operator()()' overloaded.  */
  3915. #if 1
  3916.       field = IDENTIFIER_CLASS_VALUE (name);
  3917. #else
  3918.       field = identifier_class_value (name);
  3919. #endif
  3920.  
  3921.       if (field == NULL_TREE)
  3922.     {
  3923.       error ("`this' has no member named `%s'", err_name);
  3924.       return error_mark_node;
  3925.     }
  3926.  
  3927.       if (TREE_CODE (field) == FIELD_DECL)
  3928.     {
  3929.       /* If it's a field, try overloading operator (),
  3930.          or calling if the field is a pointer-to-function.  */
  3931.       instance = build_component_ref_1 (C_C_D, field, 0, 1);
  3932.       if (instance == error_mark_node)
  3933.         return error_mark_node;
  3934.  
  3935.       if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
  3936.           && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
  3937.         return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms);
  3938.  
  3939.       if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  3940.         if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
  3941.           return build_function_call (instance, parms);
  3942.         else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
  3943.           return build_function_call (instance, tree_cons (NULL_TREE, current_class_decl, parms));
  3944.     }
  3945.       return NULL_TREE;
  3946.     }
  3947.  
  3948.   /* Check to see if this is not really a reference to an instance variable
  3949.      with `operator()()' overloaded.  */
  3950.   field = lookup_field (basetype_path, name, 1);
  3951.  
  3952.   /* This can happen if the reference was ambiguous
  3953.      or for visibility violations.  */
  3954.   if (field == error_mark_node)
  3955.     return error_mark_node;
  3956.   if (field)
  3957.     {
  3958.       tree basetype;
  3959.       tree ftype = TREE_TYPE (field);
  3960.  
  3961.       if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
  3962.     {
  3963.       /* Make the next search for this field very short.  */
  3964.       basetype = DECL_FIELD_CONTEXT (field);
  3965.       instance_ptr = convert_pointer_to (basetype, instance_ptr);
  3966.  
  3967.       instance = build_indirect_ref (instance_ptr, 0);
  3968.       return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
  3969.                  build_component_ref_1 (instance, field, 0, 0),
  3970.                  parms);
  3971.     }
  3972.       if (TREE_CODE (ftype) == POINTER_TYPE)
  3973.     {
  3974.       if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
  3975.           || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
  3976.         {
  3977.           /* This is a member which is a pointer to function.  */
  3978.           tree ref = build_component_ref_1 (build_indirect_ref (instance_ptr, 0, 0),
  3979.                         field, LOOKUP_COMPLAIN);
  3980.           if (ref == error_mark_node)
  3981.         return error_mark_node;
  3982.           return build_function_call (ref, parms);
  3983.         }
  3984.     }
  3985.       else if (TREE_CODE (ftype) == METHOD_TYPE)
  3986.     {
  3987.       error ("invalid call via pointer-to-member function");
  3988.       return error_mark_node;
  3989.     }
  3990.       else
  3991.     return NULL_TREE;
  3992.     }
  3993.   return NULL_TREE;
  3994. }
  3995.  
  3996. /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
  3997.    This is how virtual function calls are avoided.  */
  3998. tree
  3999. build_scoped_method_call (exp, scopes, name, parms)
  4000.      tree exp;
  4001.      tree scopes;
  4002.      tree name;
  4003.      tree parms;
  4004. {
  4005.   /* Because this syntactic form does not allow
  4006.      a pointer to a base class to be `stolen',
  4007.      we need not protect the drived->base conversion
  4008.      that happens here.
  4009.      
  4010.      @@ But we do have to check visibility privileges later.  */
  4011.   tree basename = (TREE_CODE (scopes) == SCOPE_REF) ? TREE_OPERAND (scopes, 1) : scopes;
  4012.   tree basetype, decl;
  4013.   tree type = TREE_TYPE (exp);
  4014.  
  4015.   if (type == error_mark_node
  4016.       || ! is_aggr_typedef (basename, 1))
  4017.     return error_mark_node;
  4018.  
  4019.   if (! IS_AGGR_TYPE (type))
  4020.     {
  4021.       error ("base object of scoped method call is not of aggregate type");
  4022.       return error_mark_node;
  4023.     }
  4024.  
  4025.   basetype = TREE_TYPE (TREE_TYPE (basename));
  4026.  
  4027.   if (basetype = basetype_or_else (basetype, type))
  4028.     {
  4029.       if (basetype == error_mark_node)
  4030.     return error_mark_node;
  4031.       if (TREE_CODE (exp) == INDIRECT_REF)
  4032.     decl = build_indirect_ref (convert_pointer_to (basetype,
  4033.                                build_unary_op (ADDR_EXPR, exp, 0)), 0);
  4034.       else
  4035.     decl = build_scoped_ref (exp, scopes);
  4036.  
  4037.       /* Call to a destructor.  */
  4038.       if (TREE_CODE (name) == BIT_NOT_EXPR)
  4039.     {
  4040.       /* Explicit call to destructor.  */
  4041.       name = TREE_OPERAND (name, 0);
  4042.       if (! is_aggr_typedef (name, 1))
  4043.         return error_mark_node;
  4044.       if (TREE_TYPE (decl) != TREE_TYPE (TREE_TYPE (name)))
  4045.         {
  4046.           error_with_aggr_type (TREE_TYPE (decl),
  4047.                     "qualified type `%s' does not match destructor type `%s'",
  4048.                     IDENTIFIER_POINTER (name));
  4049.           return error_mark_node;
  4050.         }
  4051.       if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
  4052.         error_with_aggr_type (TREE_TYPE (decl), "type `%s' has no destructor");
  4053.       return build_delete (TREE_TYPE (decl), decl, integer_two_node,
  4054.                    LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
  4055.     }
  4056.  
  4057.       /* Call to a method.  */
  4058.       return build_method_call (decl, name, parms, NULL_TREE,
  4059.                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
  4060.     }
  4061.   return error_mark_node;
  4062. }
  4063.  
  4064. /* Build something of the form ptr->method (args)
  4065.    or object.method (args).  This can also build
  4066.    calls to constructors, and find friends.
  4067.  
  4068.    Member functions always take their class variable
  4069.    as a pointer.
  4070.  
  4071.    INSTANCE is a class instance.
  4072.  
  4073.    NAME is the NAME field of the struct, union, or class
  4074.    whose type is that of INSTANCE.
  4075.  
  4076.    PARMS help to figure out what that NAME really refers to.
  4077.  
  4078.    BASETYPE_PATH, if non-NULL, tells which basetypes of INSTANCE
  4079.    we should be traversed before starting our search.  We need
  4080.    this information to get protected accesses correct.
  4081.  
  4082.    FLAGS is the logical disjunction of zero or more LOOKUP_
  4083.    flags.  See cplus-tree.h for more info.
  4084.  
  4085.    If this is all OK, calls build_function_call with the resolved
  4086.    member function.
  4087.  
  4088.    This function must also handle being called to perform
  4089.    initialization, promotion/coercion of arguments, and
  4090.    instantiation of default parameters.
  4091.  
  4092.    Note that NAME may refer to an instance variable name.  If
  4093.    `operator()()' is defined for the type of that field, then we return
  4094.    that result.  */
  4095. tree
  4096. build_method_call (instance, name, parms, basetype_path, flags)
  4097.      tree instance, name, parms, basetype_path;
  4098.      int flags;
  4099. {
  4100.   register tree function, fntype, value_type;
  4101.   register tree basetype, save_basetype;
  4102.   register tree baselink, result, method_name, parmtypes, parm;
  4103.   tree last;
  4104.   int pass;
  4105.   enum visibility_type visibility;
  4106.   int rank_for_overload ();
  4107.  
  4108.   /* Range of cases for vtable optimization.  */
  4109.   enum vtable_needs
  4110.     {
  4111.       not_needed, maybe_needed, unneeded, needed,
  4112.     };
  4113.   enum vtable_needs need_vtbl = not_needed;
  4114.  
  4115.   char *err_name;
  4116.   char *name_kind;
  4117.   int ever_seen = 0;
  4118.   int wrap;
  4119.   tree wrap_type;
  4120.   tree instance_ptr = NULL_TREE;
  4121.   int all_virtual = flag_all_virtual;
  4122.   int static_call_context;
  4123.   tree saw_private = 0;
  4124.   tree saw_protected = 0;
  4125. #ifdef SOS
  4126.   /* If call is a call to a constructor, then `dtbl'
  4127.      will first be initialized with the function table pointer
  4128.      of the appropriate type (calling "sosFindCode" as a last
  4129.      resort), the the call to the constructor will go through there.  */
  4130.   tree dtbl = (flags & LOOKUP_DYNAMIC) ? TREE_VALUE (parms) : NULL_TREE;
  4131.  
  4132.   /* Flag saying whether or not `dtbl' has been inserted into the
  4133.      parameter list.  This is needed because we cannot tell (until
  4134.      we have a match) whether this parameter should go in or not.
  4135.  
  4136.      If 1, then `dtbl' is living naturally.
  4137.      If 0, then `dtbl' is not among the parms that we know about.
  4138.      If -1, the `dtbl' was place into the parms unnaturally.
  4139.  
  4140.      Note that we may side-effect the parameter list, but in such a way
  4141.      that the caller of this function would never know.  */
  4142.   int dtbl_inserted = (flags & LOOKUP_DYNAMIC);
  4143. #endif
  4144.  
  4145.   /* Keep track of `const' and `volatile' objects.  */
  4146.   int constp, volatilep;
  4147.  
  4148.   /* Know if this is explicit destructor call.  */
  4149.   int dtor_specd = 0;
  4150.  
  4151. #ifdef GATHER_STATISTICS
  4152.   n_build_method_call++;
  4153. #endif
  4154.  
  4155.   if (instance == error_mark_node
  4156.       || name == error_mark_node
  4157.       || parms == error_mark_node
  4158.       || (instance != 0 && TREE_TYPE (instance) == error_mark_node))
  4159.     return error_mark_node;
  4160.  
  4161. #if 0
  4162.   /* C++ 2.1 does not allow this, but ANSI probably will.  */
  4163.   if (TREE_CODE (name) == BIT_NOT_EXPR)
  4164.     {
  4165.       error ("invalid call to destructor, use qualified name `%s::~%s'",
  4166.          IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (name));
  4167.       return error_mark_node;
  4168.     }
  4169. #else
  4170.   if (TREE_CODE (name) == BIT_NOT_EXPR)
  4171.     {
  4172.       flags |= LOOKUP_DESTRUCTOR;
  4173.       name = TREE_OPERAND (name, 0);
  4174.       if (! is_aggr_typedef (name, 1))
  4175.     return error_mark_node;
  4176.       if (parms)
  4177.     error ("destructors take no parameters");
  4178.       basetype = TREE_TYPE (TREE_TYPE (name));
  4179.       if (! TYPE_HAS_DESTRUCTOR (basetype))
  4180.     error_with_aggr_type (basetype, "type `%s' has no destructor");
  4181.       instance = default_conversion (instance);
  4182.       if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  4183.     instance_ptr = instance;
  4184.       else
  4185.     instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  4186.       return build_delete (basetype, instance_ptr, integer_two_node,
  4187.                LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
  4188.     }
  4189. #endif
  4190.  
  4191.   if (TREE_CODE (name) == WRAPPER_EXPR)
  4192.     {
  4193.       wrap_type = TREE_OPERAND (name, 0);
  4194.       name = TREE_OPERAND (name, 1);
  4195.       wrap = 1;
  4196.     }
  4197.   else if (TREE_CODE (name) == ANTI_WRAPPER_EXPR)
  4198.     {
  4199.       wrap_type = TREE_OPERAND (name, 0);
  4200.       name = TREE_OPERAND (name, 1);
  4201.       wrap = -1;
  4202.     }
  4203.   else
  4204.     {
  4205.       wrap_type = NULL_TREE;
  4206.       wrap = 0;
  4207.     }
  4208.  
  4209.   /* Initialize name for error reporting.  */
  4210.   if (TREE_CODE (name) == OP_IDENTIFIER)
  4211.     name = build_operator_fnname (&name, parms, 1);
  4212.  
  4213.   if (OPERATOR_NAME_P (name))
  4214.     {
  4215.       char *p = operator_name_string (name);
  4216.       err_name = (char *)alloca (strlen (p) + 10);
  4217.       sprintf (err_name, "operator %s", p);
  4218.     }
  4219.   else if (name == wrapper_name)
  4220.     err_name = "wrapper";
  4221.   else if (OPERATOR_TYPENAME_P (name))
  4222.     err_name = "type conversion operator";
  4223.   else if (TREE_CODE (name) == SCOPE_REF)
  4224.     err_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
  4225.   else
  4226.     err_name = IDENTIFIER_POINTER (name);
  4227.  
  4228. #ifdef FIELD_XREF
  4229.   FIELD_xref_call(current_function_decl,err_name);
  4230. #endif
  4231.  
  4232.   if (wrap)
  4233.     {
  4234.       char *p = (char *)alloca (strlen (err_name) + 32);
  4235.       sprintf (p, "%s for `%s'", wrap < 0 ? "anti-wrapper" : "wrapper", err_name);
  4236.       err_name = p;
  4237.     }
  4238.  
  4239.   if (instance == NULL_TREE)
  4240.     {
  4241.       static_call_context = 0;
  4242.  
  4243.       basetype = NULL_TREE;
  4244.       /* Check cases where this is really a call to raise
  4245.      an exception.  */
  4246.       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
  4247.     {
  4248.       basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
  4249.       if (basetype)
  4250.         basetype = TREE_VALUE (basetype);
  4251.     }
  4252.       else if (TREE_CODE (name) == SCOPE_REF
  4253.            && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
  4254.     {
  4255.       if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
  4256.         return error_mark_node;
  4257.       basetype = purpose_member (TREE_OPERAND (name, 1),
  4258.                      CLASSTYPE_TAGS (TREE_TYPE (TREE_TYPE (TREE_OPERAND (name, 0)))));
  4259.       if (basetype)
  4260.         basetype = TREE_VALUE (basetype);
  4261.     }
  4262.  
  4263.       if (basetype != NULL_TREE)
  4264.     ;
  4265.       /* call to a constructor... */
  4266.       else if (TREE_TYPE (name))
  4267.     basetype = TREE_TYPE (TREE_TYPE (name));
  4268.       else
  4269.     {
  4270.       tree typedef_name = lookup_name (name);
  4271.       if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
  4272.         {
  4273.           /* Cannonicalize the typedef name.  */
  4274.           basetype = TREE_TYPE (typedef_name);
  4275.           name = DECL_NAME (TYPE_NAME (basetype));
  4276.         }
  4277.       else
  4278.         {
  4279.           error ("no constructor named `%s' in visible scope",
  4280.              IDENTIFIER_POINTER (name));
  4281.           return error_mark_node;
  4282.         }
  4283.     }
  4284.       if (wrap_type && wrap_type != basetype)
  4285.     {
  4286.       error_with_aggr_type (wrap_type, "invalid constructor `%s::%s'",
  4287.                 TYPE_NAME_STRING (basetype));
  4288.       return error_mark_node;
  4289.     }
  4290.       if (TYPE_VIRTUAL_P (basetype))
  4291.     {
  4292.       wrap_type = basetype;
  4293.     }
  4294.  
  4295.       if (! IS_AGGR_TYPE (basetype))
  4296.     {
  4297.     non_aggr_error:
  4298.       if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
  4299.         error ("request for member `%s' in something not a structure or union", err_name);
  4300.  
  4301.       return error_mark_node;
  4302.     }
  4303.     }
  4304.   else if (instance == C_C_D || instance == current_class_decl)
  4305.     {
  4306.       extern tree ctor_label, dtor_label;
  4307.  
  4308.       /* When doing initialization, we side-effect the TREE_TYPE of
  4309.      C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
  4310.       basetype = TREE_TYPE (C_C_D);
  4311.  
  4312.       /* Anything manifestly `this' in constructors and destructors
  4313.      has a known type, so virtual function tables are not needed.  */
  4314.       if (TYPE_VIRTUAL_P (basetype)
  4315.       && !(flags & LOOKUP_NONVIRTUAL)
  4316.       && wrap_type == NULL_TREE)
  4317.     need_vtbl = (dtor_label || ctor_label)
  4318.       ? unneeded : maybe_needed;
  4319.  
  4320.       static_call_context = 0;
  4321.       instance = C_C_D;
  4322.       instance_ptr = current_class_decl;
  4323.       result = build_field_call (CLASSTYPE_AS_LIST (current_class_type),
  4324.                  instance_ptr, name, parms, err_name);
  4325.  
  4326.       if (result)
  4327.     return result;
  4328.     }
  4329.   else if (TREE_CODE (instance) == RESULT_DECL)
  4330.     {
  4331.       static_call_context = 0;
  4332.       basetype = TREE_TYPE (instance);
  4333.       if (wrap_type)
  4334.     {
  4335.       if (basetype_or_else (basetype, wrap_type))
  4336.         basetype = wrap_type;
  4337.       else
  4338.         return error_mark_node;
  4339.     }
  4340.       /* Should we ever have to make a virtual function reference
  4341.      from a RESULT_DECL, know that it must be of fixed type
  4342.      within the scope of this function.  */
  4343.       else if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
  4344.     need_vtbl = maybe_needed;
  4345.       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
  4346.     }
  4347.   else if (instance == current_exception_object)
  4348.     {
  4349.       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (current_exception_type),
  4350.                 TREE_OPERAND (current_exception_object, 0));
  4351.       mark_addressable (TREE_OPERAND (current_exception_object, 0));
  4352.       result = build_field_call (CLASSTYPE_AS_LIST (current_exception_type),
  4353.                  instance_ptr, name, parms, err_name);
  4354.       if (result)
  4355.     return result;
  4356.       error ("exception member `%s' cannot be invoked", err_name);
  4357.       return error_mark_node;
  4358.     }
  4359.   else
  4360.     {
  4361.       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
  4362.       tree inst_ptr_basetype;
  4363.  
  4364.       /* from the file "cplus-typeck.c".  */
  4365.       extern tree unary_complex_lvalue ();
  4366.  
  4367.       static_call_context = (TREE_CODE (instance) == NOP_EXPR
  4368.                  && TREE_OPERAND (instance, 0) == error_mark_node);
  4369.  
  4370.       /* the base type of an instance variable is pointer to class */
  4371.       basetype = TREE_TYPE (instance);
  4372.  
  4373.       if (TREE_CODE (basetype) == REFERENCE_TYPE)
  4374.     {
  4375.       basetype = TYPE_MAIN_VARIANT (TREE_TYPE (basetype));
  4376.       if (! IS_AGGR_TYPE (basetype))
  4377.         goto non_aggr_error;
  4378.       /* Call to convert not needed because we are remaining
  4379.          within the same type.  */
  4380.       instance_ptr = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), instance);
  4381.       inst_ptr_basetype = basetype;
  4382.     }
  4383.       else
  4384.     {
  4385.       if (TREE_CODE (basetype) == POINTER_TYPE)
  4386.         {
  4387.           basetype = TREE_TYPE (basetype);
  4388.           instance_ptr = instance;
  4389.         }
  4390.  
  4391.       if (! IS_AGGR_TYPE (basetype))
  4392.         goto non_aggr_error;
  4393.  
  4394.       if (! instance_ptr)
  4395.         {
  4396.           if ((lvalue_p (instance)
  4397.            && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
  4398.           || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
  4399.         {
  4400.           if (instance_ptr == error_mark_node)
  4401.             return error_mark_node;
  4402.         }
  4403.           else if (TREE_CODE (instance) == NOP_EXPR
  4404.                || TREE_CODE (instance) == CONSTRUCTOR)
  4405.         {
  4406.           /* A cast is not an lvalue.  Initialize a fresh temp
  4407.              with the value we are casting from, and proceed with
  4408.              that temporary.  We can't cast to a reference type,
  4409.              so that simplifies the initialization to something
  4410.              we can manage.  */
  4411.           tree temp = get_temp_name (TREE_TYPE (instance), 0);
  4412.           if (IS_AGGR_TYPE (TREE_TYPE (instance)))
  4413.             expand_aggr_init (temp, instance, 0);
  4414.           else
  4415.             {
  4416.               store_init_value (temp, instance);
  4417.               expand_decl_init (temp);
  4418.             }
  4419.           instance = temp;
  4420.           instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  4421.         }
  4422.           else
  4423.         {
  4424.           assert (TREE_CODE (instance) == CALL_EXPR);
  4425.           if (TYPE_NEEDS_CONSTRUCTOR (basetype))
  4426.             instance = build_cplus_new (basetype, instance, 0);
  4427.           else
  4428.             {
  4429.               instance = get_temp_name (basetype, 0);
  4430.               TREE_ADDRESSABLE (instance) = 1;
  4431.             }
  4432.           instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  4433.         }
  4434.           /* @@ Should we call comp_target_types here?  */
  4435.           inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
  4436.           if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
  4437.         basetype = inst_ptr_basetype;
  4438.           else
  4439.         instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
  4440.         }
  4441.       else
  4442.         inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
  4443.     }
  4444.  
  4445.       if (basetype_path == NULL_TREE)
  4446.     basetype_path = CLASSTYPE_AS_LIST (inst_ptr_basetype);
  4447.  
  4448.       result = build_field_call (basetype_path, instance_ptr, name, parms, err_name);
  4449.       if (result)
  4450.     return result;
  4451.  
  4452.       if (wrap_type)
  4453.     {
  4454.       if (basetype_or_else (basetype, wrap_type))
  4455.         basetype = wrap_type;
  4456.       else
  4457.         return error_mark_node;
  4458.     }
  4459.       else if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
  4460.     {
  4461.       if (TREE_VOLATILE (instance_ptr))
  4462.         {
  4463.           /* This action is needed because the instance is needed
  4464.          for providing the base of the virtual function table.
  4465.          Without using a SAVE_EXPR, the function we are building
  4466.          may be called twice, or side effects on the instance
  4467.          variable (such as a post-increment), may happen twice.  */
  4468.           instance_ptr = save_expr (instance_ptr);
  4469.           instance = build_indirect_ref (instance_ptr, 0);
  4470.         }
  4471.       else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  4472.         {
  4473.           /* This happens when called for operator new ().  */
  4474.           instance = build_indirect_ref (instance, 0);
  4475.         }
  4476.  
  4477.       need_vtbl = maybe_needed;
  4478.     }
  4479.     }
  4480.  
  4481.   if (TYPE_SIZE (basetype) == 0)
  4482.     {
  4483.       /* This is worth complaining about, I think.  */
  4484.       error_with_aggr_type (basetype, "cannot lookup method in incomplete type `%s'");
  4485.       return error_mark_node;
  4486.     }
  4487.  
  4488.   /* Are we building a non-virtual wrapper?  */
  4489.   if (flags & LOOKUP_NONVIRTUAL)
  4490.     {
  4491.       if (all_virtual)
  4492.     sorry ("non-virtual call with -fall-virtual");
  4493.       if (wrap)
  4494.     wrap_type = basetype;
  4495.     }
  4496.  
  4497.   save_basetype = basetype;
  4498.  
  4499.   if (all_virtual == 1
  4500.       && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
  4501.              OPERATOR_METHOD_LENGTH)
  4502.       || instance_ptr == NULL_TREE
  4503.       || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0
  4504.           && TYPE_NEEDS_WRAPPER (basetype) == 0)))
  4505.     all_virtual = 0;
  4506.  
  4507.   last = NULL_TREE;
  4508.   for (parmtypes = 0, parm = parms; parm; parm = TREE_CHAIN (parm))
  4509.     {
  4510.       tree t = TREE_TYPE (TREE_VALUE (parm));
  4511.       if (TREE_CODE (t) == OFFSET_TYPE)
  4512.     {
  4513.       /* Convert OFFSET_TYPE entities to their normal selves.  */
  4514.       TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
  4515.       t = TREE_TYPE (TREE_VALUE (parm));
  4516.     }
  4517.       if (TREE_CODE (t) == ARRAY_TYPE)
  4518.     {
  4519.       /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
  4520.          This eliminates needless calls to `compute_conversion_costs'.  */
  4521.       TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
  4522.       t = TREE_TYPE (TREE_VALUE (parm));
  4523.     }
  4524.       if (t == error_mark_node)
  4525.     return error_mark_node;
  4526.       last = build_tree_list (NULL_TREE, t);
  4527.       parmtypes = chainon (parmtypes, last);
  4528.     }
  4529.  
  4530.   if (instance)
  4531.     {
  4532.       constp = TREE_READONLY (instance);
  4533.       volatilep = TREE_THIS_VOLATILE (instance);
  4534.       parms = tree_cons (NULL_TREE, instance_ptr, parms);
  4535.     }
  4536.   else
  4537.     {
  4538.       /* Raw constructors are always in charge.  */
  4539.       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
  4540.       && ! (flags & LOOKUP_HAS_IN_CHARGE))
  4541.     {
  4542.       flags |= LOOKUP_HAS_IN_CHARGE;
  4543.       parms = tree_cons (NULL_TREE, integer_one_node, parms);
  4544.       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
  4545.     }
  4546.  
  4547.       if (flag_this_is_variable)
  4548.     {
  4549.       constp = 0;
  4550.       volatilep = 0;
  4551.       parms = tree_cons (NULL_TREE, build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node), parms);
  4552.     }
  4553.       else
  4554.     {
  4555.       constp = 0;
  4556.       volatilep = 0;
  4557.       instance_ptr = build_new (NULL_TREE, basetype, void_type_node, 0);
  4558.       if (instance_ptr == error_mark_node)
  4559.         return error_mark_node;
  4560.       instance_ptr = save_expr (instance_ptr);
  4561.       TREE_CALLS_NEW (instance_ptr) = 1;
  4562.       instance = build_indirect_ref (instance_ptr, 0);
  4563.       parms = tree_cons (NULL_TREE, instance_ptr, parms);
  4564.     }
  4565.     }
  4566.   parmtypes = tree_cons (NULL_TREE,
  4567.              build_pointer_type (build_type_variant (basetype, constp, volatilep)),
  4568.              parmtypes);
  4569.   if (last == NULL_TREE)
  4570.     last = parmtypes;
  4571.  
  4572.   /* Look up function name in the structure type definition.  */
  4573.  
  4574.   if (wrap)
  4575.     {
  4576.       if (wrap > 0)
  4577.     name_kind = "wrapper";
  4578.       else
  4579.     name_kind = "anti-wrapper";
  4580.       baselink = get_wrapper (basetype);
  4581.     }
  4582.   else
  4583.     {
  4584.       if (TREE_TYPE (name)
  4585.       && TREE_CODE (TREE_TYPE (name)) == TYPE_DECL
  4586.       && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (name))))
  4587.     {
  4588.       tree tmp = NULL_TREE;
  4589.       if (TREE_TYPE (name) == TYPE_NAME (basetype))
  4590.         tmp = basetype;
  4591.       else
  4592.         tmp = get_base_type (TREE_TYPE (TREE_TYPE (name)), basetype, 0);
  4593.       if (tmp != 0)
  4594.         {
  4595.           name_kind = "constructor";
  4596.  
  4597.           if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
  4598.             && ! (flags & LOOKUP_HAS_IN_CHARGE))
  4599.         {
  4600.           /* Constructors called for initialization
  4601.              only are never in charge.  */
  4602.           tree tmplist;
  4603.  
  4604.           flags |= LOOKUP_HAS_IN_CHARGE;
  4605.           tmplist = tree_cons (NULL_TREE, integer_zero_node,
  4606.                        TREE_CHAIN (parms));
  4607.           TREE_CHAIN (parms) = tmplist;
  4608.           tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
  4609.           TREE_CHAIN (parmtypes) = tmplist;
  4610.         }
  4611.  
  4612. #ifdef SOS
  4613.           if (TYPE_DYNAMIC (basetype) && dtbl_inserted == 0)
  4614.         {
  4615.           tree parm, parmtype;
  4616.           dtbl = get_sos_dtable (basetype);
  4617.           parm = tree_cons (NULL_TREE, dtbl, TREE_CHAIN (parms));
  4618.           parmtype = tree_cons (NULL_TREE, build_pointer_type (ptr_type_node), TREE_CHAIN (parmtypes));
  4619.           TREE_CHAIN (parms) = parm;
  4620.           TREE_CHAIN (parmtypes) = parmtype;
  4621.           dtbl_inserted = -1;
  4622.         }
  4623. #endif
  4624.           /* constructors are in very specific places.  */
  4625. #ifdef SOS
  4626.           if (dtbl_inserted == -1)
  4627.         {
  4628.           TREE_CHAIN (parmtypes) = TREE_CHAIN (TREE_CHAIN (parmtypes));
  4629.           TREE_CHAIN (parms) = TREE_CHAIN (TREE_CHAIN (parms));
  4630.           dtbl_inserted = 0;
  4631.         }
  4632. #endif
  4633.           basetype = tmp;
  4634.         }
  4635.       else
  4636.         name_kind = "method";
  4637.     }
  4638.       else name_kind = "method";
  4639.  
  4640.       if (basetype_path == NULL_TREE)
  4641.     basetype_path = CLASSTYPE_AS_LIST (basetype);
  4642.       result = lookup_fnfields (basetype_path, name,
  4643.                 (flags & LOOKUP_COMPLAIN));
  4644.       if (result == error_mark_node)
  4645.     return error_mark_node;
  4646.     }
  4647.  
  4648.   /* Now, go look for this method name. We do not find destructors here.
  4649.  
  4650.      Putting `void_list_node' on the end of the parmtypes
  4651.      fakes out `build_decl_overload' into doing the right thing.  */
  4652.   TREE_CHAIN (last) = void_list_node;
  4653.   method_name = build_decl_overload (IDENTIFIER_POINTER (name),
  4654.                      parmtypes,
  4655.                      1 + (name == DECL_NAME (TYPE_NAME (save_basetype))));
  4656.   TREE_CHAIN (last) = NULL_TREE;
  4657.  
  4658.   for (pass = 0; pass < 2; pass++)
  4659.     {
  4660.       struct candidate *candidates;
  4661.       struct candidate *cp;
  4662.       int len, best = 2;
  4663.  
  4664.       /* This increments every time we go up the type hierarchy.
  4665.      The idea is to prefer a function of the derived class if possible.  */
  4666.       int b_or_d;
  4667.  
  4668.       baselink = result;
  4669.  
  4670.       if (pass > 0)
  4671.     {
  4672.       candidates = (struct candidate *) alloca ((ever_seen+1) * sizeof (struct candidate));
  4673.       cp = candidates;
  4674.       len = list_length (parms);
  4675.       b_or_d = 0;
  4676.  
  4677.       /* First see if a global function has a shot at it.  */
  4678.       if (flags & LOOKUP_GLOBAL)
  4679.         {
  4680.           tree friend_parms;
  4681.           tree parm = TREE_VALUE (parms);
  4682.  
  4683.           if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
  4684.         friend_parms = parms;
  4685.           else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
  4686.         {
  4687.           parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
  4688.           parm = convert (build_reference_type (TREE_TYPE (parm)), parm);
  4689.           friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
  4690.         }
  4691.           else
  4692.         assert (0);
  4693.  
  4694.           cp->harshness
  4695.         = (unsigned short *)alloca ((len+1) * sizeof (short));
  4696.           result = build_overload_call (name, friend_parms, 0, cp);
  4697.           /* If it turns out to be the one we were actually looking for
  4698.          (it was probably a friend function), the return the
  4699.          good result.  */
  4700.           if (TREE_CODE (result) == CALL_EXPR)
  4701.         return result;
  4702.  
  4703.           while (cp->evil == 0)
  4704.         {
  4705.           /* non-standard uses: set the field to 0 to indicate
  4706.              we are using a non-member function.  */
  4707.           cp->u.field = 0;
  4708.           if (cp->harshness[len] == 0
  4709.               && cp->harshness[len] == 0
  4710.               && cp->user == 0 && cp->b_or_d == 0
  4711.               && cp->easy < best)
  4712.             best = cp->easy;
  4713.           cp += 1;
  4714.         }
  4715.         }
  4716.     }
  4717.  
  4718.       while (baselink)
  4719.     {
  4720.       /* We have a hit (of sorts). If the parameter list is
  4721.          "error_mark_node", or some variant thereof, it won't
  4722.          match any methods. Since we have verified that the is
  4723.          some method vaguely matching this one (in name at least),
  4724.          silently return.
  4725.          
  4726.          Don't stop for friends, however.  */
  4727.       tree basetypes = TREE_PURPOSE (baselink);
  4728.  
  4729.       function = TREE_VALUE (baselink);
  4730.       basetype = TREE_VALUE (basetypes);
  4731.  
  4732.       /* Cast the instance variable to the approriate type.  */
  4733.       TREE_VALUE (parmtypes) = TYPE_POINTER_TO (basetype);
  4734.  
  4735.       if (DESTRUCTOR_NAME_P (DECL_NAME (function)))
  4736.         function = TREE_CHAIN (function);
  4737.  
  4738.       for (; function; function = TREE_CHAIN (function))
  4739.         {
  4740. #ifdef GATHER_STATISTICS
  4741.           n_inner_fields_searched++;
  4742. #endif
  4743.           ever_seen++;
  4744.  
  4745.           /* Not looking for friends here.  */
  4746.           if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
  4747.           && ! DECL_STATIC_FUNCTION_P (function))
  4748.         continue;
  4749.  
  4750.           if (pass == 0
  4751.           && DECL_NAME (function) == method_name)
  4752.         {
  4753.           if (flags & LOOKUP_PROTECT)
  4754.             {
  4755.               visibility = compute_visibility (basetypes, function);
  4756.               if (visibility == visibility_protected
  4757.               && flags & LOOKUP_PROTECTED_OK)
  4758.             visibility = visibility_public;
  4759.             }
  4760.  
  4761.           if ((flags & LOOKUP_PROTECT) == 0
  4762.               || visibility == visibility_public)
  4763.             goto found_and_ok;
  4764.           else if (visibility == visibility_private)
  4765.             saw_private = function;
  4766.           else if (visibility == visibility_protected)
  4767.             saw_protected = function;
  4768.           /* If we fail on the exact match, we have
  4769.              an immediate failure.  */
  4770.           goto found;
  4771.         }
  4772.           if (pass > 0)
  4773.         {
  4774.           tree these_parms = parms;
  4775.  
  4776. #ifdef GATHER_STATISTICS
  4777.           n_inner_fields_searched++;
  4778. #endif
  4779.           cp->harshness
  4780.             = (unsigned short *)alloca ((len+1) * sizeof (short));
  4781.           if (DECL_STATIC_FUNCTION_P (function))
  4782.             these_parms = TREE_CHAIN (these_parms);
  4783.           compute_conversion_costs (function, these_parms, cp, len);
  4784.           cp->b_or_d += b_or_d;
  4785.           if (cp->evil == 0)
  4786.             {
  4787.               cp->u.field = function;
  4788.               cp->function = function;
  4789.               if (flags & LOOKUP_PROTECT)
  4790.             {
  4791.               enum visibility_type this_v;
  4792.               this_v = compute_visibility (basetypes, function);
  4793.               if (this_v == visibility_protected
  4794.                   && (flags & LOOKUP_PROTECTED_OK))
  4795.                 this_v = visibility_public;
  4796.               if (this_v != visibility_public)
  4797.                 {
  4798.                   if (this_v == visibility_private)
  4799.                 saw_private = function;
  4800.                   else
  4801.                 saw_protected = function;
  4802.                   continue;
  4803.                 }
  4804.             }
  4805.  
  4806.               /* No "two-level" conversions.  */
  4807.               if (flags & LOOKUP_NO_CONVERSION && cp->user != 0)
  4808.             continue;
  4809.  
  4810.               /* If we used default parameters, we must
  4811.              check to see whether anyone else might
  4812.              use them also, and report a possible
  4813.              ambiguity.  */
  4814.               if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype)
  4815.               && cp->harshness[len] == 0
  4816.               && (cp->harshness[0] & 128) == 0
  4817.               && cp->user == 0 && cp->b_or_d == 0
  4818.               && cp->easy < best)
  4819.             {
  4820.               if (! DECL_STATIC_FUNCTION_P (function))
  4821.                 TREE_VALUE (parms) = cp->arg;
  4822.               if (best == 2)
  4823.                 goto found_and_maybe_warn;
  4824.             }
  4825.               cp++;
  4826.             }
  4827.         }
  4828.         }
  4829.       /* Now we have run through one link's member functions.
  4830.          arrange to head-insert this link's links.  */
  4831.       baselink = next_baselink (baselink);
  4832.       b_or_d += 1;
  4833.     }
  4834.       if (pass == 0)
  4835.     {
  4836.       /* No exact match could be found.  Now try to find match
  4837.          using default conversions.  */
  4838.       if ((flags & LOOKUP_GLOBAL) && IDENTIFIER_GLOBAL_VALUE (name))
  4839.         if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == FUNCTION_DECL)
  4840.           ever_seen += 1;
  4841.         else if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == TREE_LIST)
  4842.           ever_seen += list_length (IDENTIFIER_GLOBAL_VALUE (name));
  4843.  
  4844.       if (ever_seen == 0)
  4845.         {
  4846.           if (flags & LOOKUP_GLOBAL)
  4847.         error ("no global or member function `%s' defined", err_name);
  4848.           else
  4849.         error_with_aggr_type (save_basetype, "no member function `%s::%s'", err_name);
  4850.           return error_mark_node;
  4851.         }
  4852.       continue;
  4853.     }
  4854.  
  4855.       if (cp - candidates != 0)
  4856.     {
  4857.       /* Rank from worst to best.  Then cp will point to best one.
  4858.          Private fields have their bits flipped.  For unsigned
  4859.          numbers, this should make them look very large.
  4860.          If the best alternate has a (signed) negative value,
  4861.          then all we ever saw were private members.  */
  4862.       if (cp - candidates > 1)
  4863.         {
  4864.           cp = ideal_candidate (save_basetype, candidates,
  4865.                     cp - candidates, parms, len);
  4866.           if (cp == 0)
  4867.         {
  4868.           error ("ambiguous type conversion requested for %s `%s'",
  4869.              name_kind, err_name);
  4870.           return error_mark_node;
  4871.         }
  4872.         }
  4873.       else if (cp[-1].evil == 2)
  4874.         {
  4875.           error ("ambiguous type conversion requested for %s `%s'",
  4876.              name_kind, err_name);
  4877.           return error_mark_node;
  4878.         }
  4879.       else cp--;
  4880.  
  4881.       /* The global function was the best, so use it.  */
  4882.       if (cp->u.field == 0)
  4883.         {
  4884.           /* We must convert the instance pointer into a reference type.
  4885.          Global overloaded functions can only either take
  4886.          aggregate objects (which come for free from references)
  4887.          or reference data types anyway.  */
  4888.           TREE_VALUE (parms) = copy_node (instance_ptr);
  4889.           TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
  4890.           return build_function_call (cp->function, parms);
  4891.         }
  4892.  
  4893.       function = cp->function;
  4894.       if (DECL_STATIC_FUNCTION_P (function))
  4895.         basetype = NULL_TREE;
  4896.       else
  4897.         {
  4898.           basetype = TREE_TYPE (TREE_TYPE (cp->arg));
  4899.           TREE_VALUE (parms) = cp->arg;
  4900.         }
  4901.       goto found_and_maybe_warn;
  4902.     }
  4903.  
  4904.       if ((flags & ~LOOKUP_GLOBAL) & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
  4905.     {
  4906.       char *tag_name, *buf;
  4907.  
  4908.       if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
  4909.           == LOOKUP_SPECULATIVELY)
  4910.         return NULL_TREE;
  4911.  
  4912.       if (DECL_STATIC_FUNCTION_P (cp->function))
  4913.         parms = TREE_CHAIN (parms);
  4914.       if (ever_seen)
  4915.         {
  4916.           if (((int)saw_protected|(int)saw_private) == 0)
  4917.         {
  4918.           if (flags & LOOKUP_SPECULATIVELY)
  4919.             return NULL_TREE;
  4920.           if (static_call_context && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
  4921.             error_with_aggr_type (TREE_TYPE (TREE_TYPE (instance_ptr)),
  4922.                       "object missing in call to `%s::%s'",
  4923.                       err_name);
  4924.           else
  4925.             report_type_mismatch (cp, parms, name_kind, err_name);
  4926.         }
  4927.           else
  4928.         {
  4929.           char buf[80];
  4930.           char *msg;
  4931.           tree seen = saw_private;
  4932.  
  4933.           if (saw_private)
  4934.             if (saw_protected)
  4935.               msg = "%s %%s (and the like) are private or protected";
  4936.             else
  4937.               msg = "the %s %%s is private";
  4938.           else
  4939.             {
  4940.               msg = "the %s %%s is protected";
  4941.               seen = saw_protected;
  4942.             }
  4943.           sprintf (buf, msg, name_kind);
  4944.           error_with_decl (seen, buf);
  4945.           error ("within this context");
  4946.         }
  4947.           return error_mark_node;
  4948.         }
  4949.  
  4950.       if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
  4951.           == LOOKUP_COMPLAIN)
  4952.         {
  4953.           if (TREE_CODE (save_basetype) == RECORD_TYPE)
  4954.         tag_name = "structure";
  4955.           else
  4956.         tag_name = "union";
  4957.  
  4958.           if (wrap)
  4959.         buf = "%s has no appropriate wrapper function defined";
  4960.           else
  4961.         {
  4962.           buf = (char *)alloca (30 + strlen (err_name));
  4963.           strcpy (buf, "%s has no method named `%s'");
  4964.         }
  4965.  
  4966.           error (buf, tag_name, err_name);
  4967.           return error_mark_node;
  4968.         }
  4969.       return NULL_TREE;
  4970.     }
  4971.       continue;
  4972.  
  4973.     found_and_maybe_warn:
  4974.       if (cp->harshness[0] & 128)
  4975.     {
  4976.       if (flags & LOOKUP_COMPLAIN)
  4977.         {
  4978.           error_with_decl (cp->function, "non-const member function `%s'");
  4979.           error ("called for const object at this point in file");
  4980.         }
  4981.       /* Not good enough for a match.  */
  4982.       else return error_mark_node;
  4983.     }
  4984.       goto found_and_ok;
  4985.     }
  4986.   /* Silently return error_mark_node.  */
  4987.   return error_mark_node;
  4988.  
  4989.  found:
  4990.   if (visibility == visibility_private)
  4991.     {
  4992.       if (flags & LOOKUP_COMPLAIN)
  4993.     error (TREE_PRIVATE (function)
  4994.            ? "%s `%s' is private"
  4995.            : "%s `%s' is from private base class",
  4996.            name_kind,
  4997.            lang_printable_name (function));
  4998.       return error_mark_node;
  4999.     }
  5000.   else if (visibility == visibility_protected)
  5001.     {
  5002.       if (flags & LOOKUP_COMPLAIN)
  5003.     error (TREE_PROTECTED (function)
  5004.            ? "%s `%s' is protected"
  5005.            : "%s `%s' has protected visibility from this point",
  5006.            name_kind,
  5007.            lang_printable_name (function));
  5008.       return error_mark_node;
  5009.     }
  5010.   abort ();
  5011.  
  5012.  found_and_ok:
  5013.  
  5014.   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
  5015.      type (if it exists) is a pointer to.  */
  5016.   basetype = DECL_CONTEXT (function);
  5017.   fntype = TREE_TYPE (function);
  5018.  
  5019.   if (TREE_CODE (fntype) == POINTER_TYPE)
  5020.     fntype = TREE_TYPE (fntype);
  5021.  
  5022.   /* If we are referencing a virtual function from an object
  5023.      of effectively static type, then there is no need
  5024.      to go through the virtual function table.  */
  5025.   if (need_vtbl == maybe_needed)
  5026.     {
  5027.       int fixed_type = resolves_to_fixed_type_p (instance);
  5028.  
  5029.       if (all_virtual == 1
  5030.       && DECL_VINDEX (function)
  5031.       && may_be_remote (basetype))
  5032.     need_vtbl = needed;
  5033.       else if (DECL_VIRTUAL_P (function))
  5034.     need_vtbl = fixed_type ? unneeded : needed;
  5035.       else
  5036.     need_vtbl = not_needed;
  5037.  
  5038.       if (fixed_type && DECL_ABSTRACT_VIRTUAL_P (function))
  5039.     {
  5040.       error_with_decl (function, "invalid call to abstract function `%s'");
  5041.       return error_mark_node;
  5042.     }
  5043.     }
  5044.  
  5045.   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context)
  5046.     {
  5047.       /* Let's be nice to the user for now, and give reasonable
  5048.      default behavior.  */
  5049.       instance_ptr = current_class_decl;
  5050.       if (instance_ptr)
  5051.     {
  5052.       if (basetype != current_class_type)
  5053.         {
  5054.           basetype = get_base_type (basetype, current_class_type, 1);
  5055.           if (basetype == 0)
  5056.         {
  5057.           error_not_base_type (DECL_CONTEXT (function), current_class_type);
  5058.           return error_mark_node;
  5059.         }
  5060.           else if (basetype == error_mark_node)
  5061.         return error_mark_node;
  5062.         }
  5063.     }
  5064.       else
  5065.     {
  5066.       error_with_aggr_type (basetype, "cannot call member function `%s::%s' without object",
  5067.                 err_name);
  5068.       return error_mark_node;
  5069.     }
  5070.     }
  5071.  
  5072.   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
  5073.  
  5074.   if (TYPE_SIZE (value_type) == 0)
  5075.     {
  5076.       if (flags & LOOKUP_COMPLAIN)
  5077.     incomplete_type_error (0, value_type);
  5078.       return error_mark_node;
  5079.     }
  5080.  
  5081.   /* We do not pass FUNCTION into `actualparameterlist', because by
  5082.      now everything should be ok.  If not, then we have a serious error.  */
  5083.   if (DECL_STATIC_FUNCTION_P (function))
  5084.     parms = actualparameterlist (NULL_TREE, TYPE_ARG_TYPES (fntype),
  5085.                  TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL);
  5086.   else if (need_vtbl == unneeded)
  5087.     {
  5088.       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
  5089.       basetype = TREE_TYPE (instance);
  5090.       if (DECL_CONTEXT (function) != TYPE_MAIN_VARIANT (basetype)
  5091.       && (TYPE_USES_MULTIPLE_INHERITANCE (basetype)
  5092.           || TYPE_USES_VIRTUAL_BASECLASSES (basetype)))
  5093.     {
  5094.       basetype = DECL_CONTEXT (function);
  5095.       instance_ptr = convert_pointer_to (basetype, instance_ptr);
  5096.       instance = build_indirect_ref (instance_ptr, 0);
  5097.     }
  5098.       parms = tree_cons (NULL_TREE, instance_ptr,
  5099.              actualparameterlist (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, sub_flags));
  5100.     }
  5101.   else
  5102.     {
  5103.       if ((flags & LOOKUP_NONVIRTUAL) == 0)
  5104.     basetype = DECL_VCONTEXT (function);
  5105.  
  5106.       /* First parm could be integer_zerop with casts like
  5107.      ((Object*)0)->Object::IsA()  */
  5108.       if (!integer_zerop (TREE_VALUE (parms)))
  5109.     {
  5110.       instance_ptr = convert_pointer_to (build_type_variant (basetype, constp, volatilep),
  5111.                          TREE_VALUE (parms));
  5112.       if (TREE_CODE (instance_ptr) == COND_EXPR)
  5113.         {
  5114.           instance_ptr = save_expr (instance_ptr);
  5115.           instance = build_indirect_ref (instance_ptr);
  5116.         }
  5117.       else if (TREE_CODE (instance_ptr) == NOP_EXPR
  5118.            && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
  5119.            && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
  5120.         ;
  5121.       else if (instance == NULL_TREE
  5122.            || TREE_CODE (instance) != INDIRECT_REF
  5123.            || TREE_OPERAND (instance, 0) != instance_ptr)
  5124.         instance = build_indirect_ref (instance_ptr);
  5125.     }
  5126.       parms = tree_cons (NULL_TREE, instance_ptr,
  5127.              actualparameterlist (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL));
  5128.     }
  5129.  
  5130.   /* See if there is a wrapper for this thing.  */
  5131.   if (wrap < 0
  5132.       || static_call_context
  5133.       || name == wrapper_name
  5134.       || name == DECL_NAME (TYPE_NAME (basetype)))
  5135.     ;
  5136.   else if (wrap > 0 || TYPE_NEEDS_WRAPPER (basetype))
  5137.     {
  5138.       flags &= ~LOOKUP_PROTECT;
  5139.       if (wrap == 0)
  5140.     {
  5141.       wrap = TYPE_NEEDS_WRAPPER (basetype);
  5142.       /* If no wrapper specified, wrapper may be virtual.  */
  5143.       flags &= ~LOOKUP_NONVIRTUAL;
  5144.     }
  5145.  
  5146.       if (wrap)
  5147.     {
  5148.       tree wrapped_result, unwrapped_result;
  5149.       register int bytecount = get_arglist_len_in_bytes (parms);
  5150.  
  5151.       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
  5152.         parm = build_unary_op (ADDR_EXPR, function, 0);
  5153.       else
  5154.         {
  5155.               fntype = build_cplus_method_type (basetype, TREE_TYPE (fntype), TYPE_ARG_TYPES (fntype));
  5156.           parm = build1 (NOP_EXPR, build_pointer_type (fntype), DECL_VINDEX (function));
  5157.         }
  5158.  
  5159.       if (TYPE_HAS_WRAPPER_PRED (basetype))
  5160.         {
  5161.           unwrapped_result = build_nt (CALL_EXPR, default_conversion (function), parms, NULL_TREE);
  5162.  
  5163.           assert (TREE_OPERAND (unwrapped_result, 1) != error_mark_node);
  5164.  
  5165.           TREE_TYPE (unwrapped_result) = value_type;
  5166.           TREE_VOLATILE (unwrapped_result) = 1;
  5167.           TREE_RAISES (unwrapped_result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
  5168.         }
  5169.  
  5170.       /* If this pointer walked as a result of multiple inheritance,
  5171.          keep its displaced value.  */
  5172.       parms = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
  5173.                  tree_cons (NULL_TREE, parm, TREE_CHAIN (parms)));
  5174.  
  5175.       wrapped_result = get_wrapper (basetype);
  5176.       assert (wrapped_result != NULL_TREE);
  5177.       assert (wrapped_result != error_mark_node);
  5178.  
  5179.       /* @@ Should BASETYPE_PATH get TREE_PURPOSE (wrapped_result) here?  */
  5180.       wrapped_result
  5181.         = build_method_call (instance,
  5182.                  DECL_ORIGINAL_NAME (TREE_VALUE (wrapped_result)),
  5183.                  parms, basetype_path, flags);
  5184. #if 0
  5185.       /* Do this if we want the result of operator->() to inherit
  5186.          the type of the function it is subbing for.  */
  5187.       if (wrapped_result != error_mark_node)
  5188.         TREE_TYPE (wrapped_result) = value_type;
  5189. #endif
  5190.  
  5191.       if (TYPE_HAS_WRAPPER_PRED (basetype))
  5192.         {
  5193.           result = build_conditional_expr
  5194.         (build_method_call (instance, wrapper_pred_name, build_tree_list (NULL_TREE, parm), basetype_path, LOOKUP_NORMAL),
  5195.          wrapped_result,
  5196.          unwrapped_result);
  5197.  
  5198.         }
  5199.       else
  5200.         {
  5201.           result = wrapped_result;
  5202.         }
  5203.  
  5204.       TREE_VOLATILE (result) = 1;
  5205.       return result;
  5206.     }
  5207.     }
  5208.   /* Constructors do not overload method calls.  */
  5209.   else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
  5210.        && name != DECL_NAME (TYPE_NAME (basetype))
  5211.        && (TREE_CODE (function) != FUNCTION_DECL
  5212.            || strncmp (IDENTIFIER_POINTER (DECL_NAME (function)),
  5213.                OPERATOR_METHOD_FORMAT,
  5214.                OPERATOR_METHOD_LENGTH))
  5215. #if 0
  5216.        && (may_be_remote (basetype)
  5217.            || (C_C_D ? TREE_TYPE (instance) != current_class_type : 1))
  5218. #else
  5219.        /* This change by Larry Ketcham.  */
  5220.          && (may_be_remote (basetype) || instance != C_C_D)
  5221. #endif
  5222.        )
  5223.     {
  5224. #ifdef ESKIT
  5225.       register int bytecount = 0;
  5226. #else
  5227.       register int bytecount = get_arglist_len_in_bytes (parms);
  5228. #endif
  5229.       tree fn_as_int;
  5230.  
  5231.       parms = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
  5232.              TREE_CHAIN (parms));
  5233.  
  5234.       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
  5235.     fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
  5236.       else
  5237.     fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
  5238.       if (all_virtual == 1)
  5239.     fn_as_int = convert (integer_type_node, fn_as_int);
  5240.  
  5241.       result = build_opfncall (METHOD_CALL_EXPR, LOOKUP_NORMAL, instance, fn_as_int, parms);
  5242.  
  5243.       if (result == NULL_TREE)
  5244.     {
  5245.       compiler_error ("could not overload `operator->()(...)'");
  5246.       return error_mark_node;
  5247.     }
  5248.       else if (result == error_mark_node)
  5249.     return error_mark_node;
  5250.  
  5251. #if 0
  5252.       /* Do this if we want the result of operator->() to inherit
  5253.      the type of the function it is subbing for.  */
  5254.       TREE_TYPE (result) = value_type;
  5255. #endif
  5256.  
  5257. #ifdef ESKIT
  5258.       {
  5259.     int used, size;
  5260.  
  5261.     /* Count the number of bytes of arguements to operator->(),
  5262.        not to the method itself.  In the tally, don't count bytes
  5263.        for pointer to member function or for the bytecount.  */
  5264.     parms = TREE_OPERAND (result, 1);
  5265.     bytecount = get_arglist_len_in_bytes (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (parms))));
  5266.     used = size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_VALUE (parms))));
  5267. #ifdef PUSH_ROUNDING
  5268.     size = PUSH_ROUNDING (size);
  5269. #endif
  5270.     used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
  5271.          / (PARM_BOUNDARY / BITS_PER_UNIT))
  5272.         * (PARM_BOUNDARY / BITS_PER_UNIT));
  5273.     bytecount += used;
  5274.     TREE_CHAIN (TREE_CHAIN (parms))
  5275.       = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
  5276.                TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (parms))));
  5277.       }
  5278. #endif
  5279.  
  5280.       return result;
  5281.     }
  5282.  
  5283.   if (need_vtbl == needed)
  5284.     {
  5285.       function = build_vfn_ref (&TREE_VALUE (parms), instance, DECL_VINDEX (function));
  5286.       TREE_TYPE (function) = build_pointer_type (fntype);
  5287.     }
  5288. #ifdef SOS
  5289.   else if (basetype && TYPE_DYNAMIC (basetype))
  5290.     {
  5291.       function = build_array_ref (dtbl, DECL_DINDEX (function));
  5292.       TREE_TYPE (function) = build_pointer_type (fntype);
  5293.     }
  5294. #endif
  5295.  
  5296.   if (TREE_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
  5297.     function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
  5298.   else function = default_conversion (function);
  5299.  
  5300.   result =
  5301.     build_nt (CALL_EXPR, function, parms, NULL_TREE);
  5302.  
  5303.   TREE_TYPE (result) = value_type;
  5304.   TREE_VOLATILE (result) = 1;
  5305.   TREE_RAISES (result)
  5306.     = TYPE_RAISES_EXCEPTIONS (fntype) || (parms && TREE_RAISES (parms));
  5307.   return result;
  5308. }
  5309.  
  5310. /* Similar to `build_method_call', but for overloaded non-member functions.
  5311.    The name of this function comes through NAME.  The name depends
  5312.    on PARMS.
  5313.  
  5314.    Note that this function must handle simple `C' promotions,
  5315.    as well as variable numbers of arguments (...), and
  5316.    default arguments to boot.
  5317.  
  5318.    If the overloading is successful, we return a treenode which
  5319.    contains the call to the function.
  5320.  
  5321.    If overloading produces candidates which are probabe, but not definite,
  5322.    we hold these candidates.  If FINAL_CP is non-zero, then we are free
  5323.    to assume that final_cp points to enough storage for all candidates that
  5324.    this function might generate.  The `harshness' array is preallocated for
  5325.    the first candidate, but not for subsequent ones.
  5326.  
  5327.    Note that the DECL_RTL of FUNCTION must be made to agree with this
  5328.    function's new name.  */
  5329.  
  5330. tree
  5331. build_overload_call (fnname, parms, complain, final_cp)
  5332.      tree fnname, parms;
  5333.      int complain;
  5334.      struct candidate *final_cp;
  5335. {
  5336.   /* must check for overloading here */
  5337.   tree overload_name, functions, function, parm;
  5338.   tree parmtypes = NULL_TREE, last = NULL_TREE;
  5339.   register tree outer;
  5340.   int length;
  5341.   int parmlength = list_length (parms);
  5342.  
  5343.   struct candidate *candidates, *cp;
  5344.   int rank_for_overload ();
  5345.  
  5346.   if (final_cp)
  5347.     {
  5348.       final_cp[0].evil = 0;
  5349.       final_cp[0].user = 0;
  5350.       final_cp[0].b_or_d = 0;
  5351.       final_cp[0].easy = 0;
  5352.       final_cp[0].function = 0;
  5353.       /* end marker.  */
  5354.       final_cp[1].evil = 1;
  5355.     }
  5356.  
  5357.   for (parm = parms; parm; parm = TREE_CHAIN (parm))
  5358.     {
  5359.       register tree t = TREE_TYPE (TREE_VALUE (parm));
  5360.  
  5361.       if (t == error_mark_node)
  5362.     return error_mark_node;
  5363.       if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == OFFSET_TYPE)
  5364.     {
  5365.       /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
  5366.          Also convert OFFSET_TYPE entities to their normal selves.
  5367.          This eliminates needless calls to `compute_conversion_costs'.  */
  5368.       TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
  5369.       t = TREE_TYPE (TREE_VALUE (parm));
  5370.     }
  5371.       last = build_tree_list (NULL_TREE, t);
  5372.       parmtypes = chainon (parmtypes, last);
  5373.     }
  5374.   if (last)
  5375.     TREE_CHAIN (last) = void_list_node;
  5376.   else
  5377.     parmtypes = void_list_node;
  5378.   overload_name = build_decl_overload (IDENTIFIER_POINTER (fnname), parmtypes, 0);
  5379.  
  5380.   /* Now check to see whether or not we can win.
  5381.      Note that if we are called from `build_method_call',
  5382.      then we cannot have a mis-match, because we would have
  5383.      already found such a winning case.  */
  5384.  
  5385.   if (IDENTIFIER_GLOBAL_VALUE (overload_name))
  5386.     if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (overload_name)) != TREE_LIST)
  5387.       return build_function_call (DECL_MAIN_VARIANT (IDENTIFIER_GLOBAL_VALUE (overload_name)), parms);
  5388.  
  5389.   functions = IDENTIFIER_GLOBAL_VALUE (fnname);
  5390.  
  5391.   if (functions == NULL_TREE)
  5392.     {
  5393.       if (complain)
  5394.     error ("only member functions apply");
  5395.       if (final_cp)
  5396.     final_cp->evil = 1;
  5397.       return error_mark_node;
  5398.     }
  5399.  
  5400.   if (TREE_CODE (functions) == FUNCTION_DECL)
  5401.     {
  5402.       functions = DECL_MAIN_VARIANT (functions);
  5403.       if (final_cp)
  5404.     {
  5405.       /* We are just curious whether this is a viable alternative or not.  */
  5406.       compute_conversion_costs (functions, parms, final_cp, parmlength);
  5407.       return functions;
  5408.     }
  5409.       else
  5410.     return build_function_call (functions, parms);
  5411.     }
  5412.  
  5413.   if (TREE_VALUE (functions) == NULL_TREE)
  5414.     {
  5415.       if (complain)
  5416.     error ("function `%s' declared overloaded, but no instances of that function declared",
  5417.            IDENTIFIER_POINTER (TREE_PURPOSE (functions)));
  5418.       return error_mark_node;
  5419.     }
  5420.  
  5421.   if (TREE_CODE (TREE_VALUE (functions)) == TREE_LIST)
  5422.     {
  5423.       register tree outer;
  5424.       length = 0;
  5425.  
  5426.       /* The list-of-lists should only occur for class things.  */
  5427.       assert (functions == IDENTIFIER_CLASS_VALUE (fnname));
  5428.  
  5429.       for (outer = functions; outer; outer = TREE_CHAIN (outer))
  5430.     {
  5431.       /* member functions.  */
  5432.       length += list_length (TREE_VALUE (TREE_VALUE (outer)));
  5433.       /* friend functions.  */
  5434.       length += list_length (TREE_TYPE (TREE_VALUE (outer)));
  5435.     }
  5436.     }
  5437.   else
  5438.     {
  5439.       length = list_length (functions);
  5440.     }
  5441.  
  5442.   if (final_cp)
  5443.     candidates = final_cp;
  5444.   else
  5445.     candidates = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
  5446.  
  5447.   cp = candidates;
  5448.  
  5449.   assert (TREE_CODE (TREE_VALUE (functions)) != TREE_LIST);
  5450.   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
  5451.  
  5452.   for (outer = functions; outer; outer = TREE_CHAIN (outer))
  5453.     {
  5454.       function = TREE_VALUE (outer);
  5455.       if (TREE_CODE (function) != FUNCTION_DECL)
  5456.     {
  5457.       if (TREE_CODE (function) == CONST_DECL)
  5458.         error_with_decl (function, "enumeral value `%s' conflicts with function of same name");
  5459.       else if (TREE_CODE (function) == VAR_DECL)
  5460.         if (TREE_STATIC (function))
  5461.           error_with_decl (function, "variable `%s' conflicts with function of same name");
  5462.         else
  5463.           error_with_decl (function, "constant field `%s' conflicts with function of same name");
  5464.       else if (TREE_CODE (function) == TYPE_DECL)
  5465.         continue;
  5466.       else abort ();
  5467.       error ("at this point in file");
  5468.       continue;
  5469.     }
  5470.       function = DECL_MAIN_VARIANT (function);
  5471.       /* Can't use alloca here, since result might be
  5472.      passed to calling function.  */
  5473.       cp->harshness
  5474.     = (unsigned short *)oballoc ((parmlength+1) * sizeof (short));
  5475.       compute_conversion_costs (function, parms, cp, parmlength);
  5476.       if (cp[0].evil == 0)
  5477.     {
  5478.       cp[1].evil = 1;
  5479.       if (final_cp
  5480.           && cp[0].user == 0 && cp[0].b_or_d == 0
  5481.           && cp[0].easy <= 1)
  5482.         {
  5483.           final_cp[0].easy = cp[0].easy;
  5484.           return function;
  5485.         }
  5486.       cp++;
  5487.     }
  5488.     }
  5489.  
  5490.   if (cp - candidates)
  5491.     {
  5492.       tree rval = error_mark_node;
  5493.  
  5494.       /* Leave marker.  */
  5495.       cp[0].evil = 1;
  5496.       if (cp - candidates > 1)
  5497.     {
  5498.       struct candidate *best_cp
  5499.         = ideal_candidate (NULL_TREE, candidates,
  5500.                    cp - candidates, parms, parmlength);
  5501.       if (best_cp == 0)
  5502.         {
  5503.           if (complain)
  5504.         error ("call of overloaded `%s' is ambiguous", IDENTIFIER_POINTER (fnname));
  5505.           return error_mark_node;
  5506.         }
  5507.       else
  5508.         rval = best_cp->function;
  5509.     }
  5510.       else
  5511.     {
  5512.       cp -= 1;
  5513.       if (cp->evil > 1)
  5514.         {
  5515.           if (complain)
  5516.         error ("type conversion ambiguous");
  5517.         }
  5518.       else
  5519.         rval = cp->function;
  5520.     }
  5521.  
  5522.       if (final_cp)
  5523.     return rval;
  5524.  
  5525.       return build_function_call (rval, parms);
  5526.     }
  5527.   else if (complain)
  5528.     {
  5529.       tree name;
  5530.       char *err_name;
  5531.       /* Initialize name for error reporting.  */
  5532.       if (TREE_CODE (functions) == TREE_LIST)
  5533.     name = TREE_PURPOSE (functions);
  5534.       else
  5535.     name = DECL_ORIGINAL_NAME (functions);
  5536.  
  5537.       if (OPERATOR_NAME_P (name))
  5538.     {
  5539.       char *opname = operator_name_string (name);
  5540.       err_name = (char *)alloca (strlen (opname) + 12);
  5541.       sprintf (err_name, "operator %s", opname);
  5542.     }
  5543.       else
  5544.     err_name = IDENTIFIER_POINTER (name);
  5545.  
  5546.       report_type_mismatch (cp, parms, "function", err_name);
  5547.     }
  5548.   return error_mark_node;
  5549. }
  5550.  
  5551. void
  5552. init_class_processing ()
  5553. {
  5554.   current_class_stacksize = 10;
  5555.   current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree));
  5556.   current_class_stack = current_class_base;
  5557.  
  5558.   current_lang_stacksize = 10;
  5559.   current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
  5560.   current_lang_stack = current_lang_base;
  5561.  
  5562.   delta_name = get_identifier (VTABLE_DELTA_NAME);
  5563.   pfn_name = get_identifier (VTABLE_PFN_NAME);
  5564.  
  5565.   /* Keep these values lying around.  */
  5566.   minus_one_node = build_int_2 (-1, 0);
  5567.   the_null_vtable_entry = build_vtable_entry (integer_zero_node, integer_zero_node);
  5568.   base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
  5569.   TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
  5570.  
  5571.   obstack_init (&class_obstack);
  5572. }
  5573.  
  5574. /* Set current scope to NAME. CODE tells us if this is a
  5575.    STRUCT, UNION, or ENUM environment.
  5576.  
  5577.    NAME may end up being NULL_TREE if this is an anonymous or
  5578.    late-bound struct (as in "struct { ... } foo;")  */
  5579.  
  5580. /* Here's a subroutine we need because C lacks lambdas.  */
  5581. void
  5582. unuse_fields (type)
  5583.      tree type;
  5584. {
  5585.   tree fields;
  5586.  
  5587.   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  5588.     {
  5589.       if (TREE_CODE (fields) != FIELD_DECL)
  5590.     continue;
  5591.  
  5592.       TREE_USED (fields) = 0;
  5593.       if (DECL_ANON_UNION_ELEM (fields))
  5594.     unuse_fields (TREE_TYPE (fields));
  5595.     }
  5596. }
  5597.  
  5598. /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
  5599.    appropriate values, found by looking up the type definition of
  5600.    NAME (as a CODE).
  5601.  
  5602.    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
  5603.    which can be seen locally to the class. They are shadowed by
  5604.    any subsequent local declaration (including parameter names).
  5605.  
  5606.    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
  5607.    which have static meaning (i.e., static members, static
  5608.    member functions, enum declarations, etc).
  5609.  
  5610.    So that we may avoid calls to lookup_name, we cache the TYPE_DECL
  5611.    in the TREE_TYPE field of the name.
  5612.  
  5613.    For multiple inheritance, we perform a two-pass depth-first search
  5614.    of the type lattice.  The first pass performs a pre-order search,
  5615.    marking types after the type has had its fields installed in
  5616.    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
  5617.    unmarks the marked types.  If a field or member function name
  5618.    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
  5619.    that name becomes `error_mark_node'.  */
  5620.  
  5621. void
  5622. pushclass (type, modify)
  5623.      tree type;
  5624.      int modify;
  5625. {
  5626.   push_memoized_context (type, modify);
  5627.  
  5628.   *current_class_stack++ = current_class_name;
  5629.   *current_class_stack++ = current_class_type;
  5630.   if (current_class_stack >= current_class_base + current_class_stacksize)
  5631.     {
  5632.       current_class_base =
  5633.     (tree *)xrealloc (current_class_base,
  5634.               sizeof (tree) * (current_class_stacksize + 10));
  5635.       current_class_stack = current_class_base + current_class_stacksize;
  5636.       current_class_stacksize += 10;
  5637.     }
  5638.  
  5639.   type = TYPE_MAIN_VARIANT (type);
  5640.   current_class_name = TYPE_NAME (type);
  5641.   if (TREE_CODE (current_class_name) == TYPE_DECL)
  5642.     current_class_name = DECL_NAME (current_class_name);
  5643.   current_class_type = type;
  5644.  
  5645.   if (type != prev_class_type && prev_class_type != NULL_TREE
  5646.       && current_class_stack == current_class_base + 2)
  5647.     {
  5648.       popclass (-1);
  5649.       prev_class_type = 0;
  5650.     }
  5651.  
  5652.   if (modify)
  5653.     {
  5654.       tree tags;
  5655.  
  5656.       if (type != prev_class_type)
  5657.     {
  5658.       build_mi_matrix (type);
  5659.       push_class_decls (type);
  5660.       free_mi_matrix ();
  5661.       prev_class_type = type;
  5662.     }
  5663.       else
  5664.     unuse_fields (type);
  5665.  
  5666.       tags = CLASSTYPE_TAGS (type);
  5667.       while (tags)
  5668.     {
  5669.       TREE_NONLOCAL (TREE_VALUE (tags)) = 1;
  5670.       pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags));
  5671.       if (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) == NULL_TREE
  5672.           && TREE_CODE (TYPE_NAME (TREE_VALUE (tags))) == TYPE_DECL)
  5673.         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags))
  5674.           = TYPE_NAME (TREE_VALUE (tags));
  5675.       tags = TREE_CHAIN (tags);
  5676.     }
  5677.     }
  5678.   else
  5679.     pushlevel_class ();
  5680. }
  5681.  
  5682. /* Get out of the current class scope. If we were in a class scope
  5683.    previously, that is the one popped to.  The flag MODIFY tells
  5684.    whether the current scope declarations needs to be modified
  5685.    as a result of popping to the new scope.  */
  5686. void
  5687. popclass (modify)
  5688.      int modify;
  5689. {
  5690.   if (modify < 0)
  5691.     {
  5692.       /* Back this old class out completely.  */
  5693.       tree tags = CLASSTYPE_TAGS (prev_class_type);
  5694.  
  5695.       pop_class_decls (prev_class_type);
  5696.       while (tags)
  5697.     {
  5698.       TREE_NONLOCAL (TREE_VALUE (tags)) = 0;
  5699.       IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
  5700.       tags = TREE_CHAIN (tags);
  5701.     }
  5702.       return;
  5703.     }
  5704.   if (modify)
  5705.     {
  5706.       /* Just remove from this class what didn't make
  5707.      it into IDENTIFIER_CLASS_VALUE.  */
  5708.       tree tags = CLASSTYPE_TAGS (current_class_type);
  5709.  
  5710.       while (tags)
  5711.     {
  5712.       TREE_NONLOCAL (TREE_VALUE (tags)) = 0;
  5713.       IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
  5714.       tags = TREE_CHAIN (tags);
  5715.     }
  5716.     }
  5717.   else
  5718.     poplevel_class ();
  5719.  
  5720.   current_class_type = *--current_class_stack;
  5721.   current_class_name = *--current_class_stack;
  5722.  
  5723.   if (current_class_type)
  5724.     {
  5725.       if (CLASSTYPE_VTBL_PTR (current_class_type))
  5726.     {
  5727.       current_vtable_decl = lookup_name (DECL_NAME (CLASSTYPE_VTBL_PTR (current_class_type)));
  5728.       if (current_vtable_decl)
  5729.         current_vtable_decl = build_indirect_ref (current_vtable_decl, 0);
  5730.     }
  5731.       current_class_decl = lookup_name (get_identifier (THIS_NAME));
  5732.       if (current_class_decl)
  5733.     {
  5734.       if (TREE_CODE (TREE_TYPE (current_class_decl)) == POINTER_TYPE)
  5735.         {
  5736.           /* Can't call build_indirect_ref here, because it has special
  5737.          logic to return C_C_D given this argument.  */
  5738.           C_C_D = build1 (INDIRECT_REF, current_class_type, current_class_decl);
  5739.           TREE_READONLY (C_C_D) = TREE_READONLY (TREE_TYPE (TREE_TYPE (current_class_decl)));
  5740.           TREE_VOLATILE (C_C_D) = TREE_VOLATILE (TREE_TYPE (TREE_TYPE (current_class_decl)));
  5741.         }
  5742.       else
  5743.         C_C_D = current_class_decl;
  5744.     }
  5745.       else C_C_D = NULL_TREE;
  5746.     }
  5747.   else
  5748.     {
  5749.       current_class_decl = NULL_TREE;
  5750.       current_vtable_decl = NULL_TREE;
  5751.       C_C_D = NULL_TREE;
  5752.     }
  5753.  
  5754.   pop_memoized_context (modify);
  5755. }
  5756.  
  5757. /* Set global variables CURRENT_LANG_NAME to appropriate value
  5758.    so that behavior of name-mangline machinery is correct.  */
  5759.  
  5760. void
  5761. push_lang_context (name)
  5762.      tree name;
  5763. {
  5764.   *current_lang_stack++ = current_lang_name;
  5765.   if (current_lang_stack >= current_lang_base + current_lang_stacksize)
  5766.     {
  5767.       current_lang_base =
  5768.     (tree *)xrealloc (current_lang_base,
  5769.               sizeof (tree) * (current_lang_stacksize + 10));
  5770.       current_lang_stack = current_lang_base + current_lang_stacksize;
  5771.       current_lang_stacksize += 10;
  5772.     }
  5773.  
  5774.   if (name == lang_name_cplusplus)
  5775.     {
  5776.       strict_prototype = strict_prototypes_lang_cplusplus;
  5777.       current_lang_name = name;
  5778.     }
  5779.   else if (name == lang_name_c)
  5780.     {
  5781.       strict_prototype = strict_prototypes_lang_c;
  5782.       current_lang_name = name;
  5783.     }
  5784.   else
  5785.     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
  5786. }
  5787.   
  5788. /* Get out of the current language scope.  */
  5789. void
  5790. pop_lang_context ()
  5791. {
  5792.   current_lang_name = *--current_lang_stack;
  5793.   if (current_lang_name == lang_name_cplusplus)
  5794.     strict_prototype = strict_prototypes_lang_cplusplus;
  5795.   else if (current_lang_name == lang_name_c)
  5796.     strict_prototype = strict_prototypes_lang_c;
  5797. }
  5798.  
  5799. int
  5800. root_lang_context_p ()
  5801. {
  5802.   return current_lang_stack == current_lang_base;
  5803. }
  5804.  
  5805. /* Type instantiation routines.  */
  5806.  
  5807. /* This function will instantiate the type of the expression given
  5808.    in RHS to match the type of LHSTYPE.  If LHSTYPE is NULL_TREE,
  5809.    or other errors exist, the TREE_TYPE of RHS will be ERROR_MARK_NODE.
  5810.  
  5811.    This function is used in build_modify_expr, actualparameterlist,
  5812.    build_c_cast, and compute_conversion_costs.  */
  5813. tree
  5814. instantiate_type (lhstype, rhs, complain)
  5815.      tree lhstype, rhs;
  5816.      int complain;
  5817. {
  5818.   if (TREE_CODE (rhs) == OP_IDENTIFIER)
  5819.     return build_instantiated_decl (lhstype, rhs);
  5820.  
  5821.   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
  5822.     {
  5823.       if (complain)
  5824.     error ("not enough type information");
  5825.       return error_mark_node;
  5826.     }
  5827.  
  5828.   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
  5829.     return rhs;
  5830.  
  5831.   /* This should really only be used when attempting to distinguish
  5832.      what sort of a pointer to function we have.  For now, any
  5833.      arithmethic operation which is not supported on pointers
  5834.      is rejected as an error.  */
  5835.  
  5836.   switch (TREE_CODE (rhs))
  5837.     {
  5838.     case TYPE_EXPR:
  5839.     case CONVERT_EXPR:
  5840.     case SAVE_EXPR:
  5841.     case CONSTRUCTOR:
  5842.     case BUFFER_REF:
  5843.       assert (0);
  5844.       return error_mark_node;
  5845.  
  5846.     case INDIRECT_REF:
  5847.     case ARRAY_REF:
  5848.       TREE_TYPE (rhs) = lhstype;
  5849.       lhstype = build_pointer_type (lhstype);
  5850.       TREE_OPERAND (rhs, 0)
  5851.     = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  5852.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  5853.     return error_mark_node;
  5854.  
  5855.       return rhs;
  5856.  
  5857.     case NOP_EXPR:
  5858.       rhs = copy_node (TREE_OPERAND (rhs, 0));
  5859.       TREE_TYPE (rhs) = unknown_type_node;
  5860.       return instantiate_type (lhstype, rhs, complain);
  5861.  
  5862.     case COMPONENT_REF:
  5863.       {
  5864.     tree field = TREE_OPERAND (rhs, 1);
  5865.     if (TREE_CODE (field) == TREE_LIST)
  5866.       {
  5867.         tree function = instantiate_type (lhstype, field, complain);
  5868.         if (function == error_mark_node)
  5869.           return error_mark_node;
  5870.         assert (TREE_CODE (function) == FUNCTION_DECL);
  5871.         if (DECL_VIRTUAL_P (function))
  5872.           {
  5873.         tree base = TREE_OPERAND (rhs, 0);
  5874.         tree base_ptr = build_unary_op (ADDR_EXPR, base, 0);
  5875.         if (base_ptr == error_mark_node)
  5876.           return error_mark_node;
  5877.         base_ptr = convert_pointer_to (DECL_VCONTEXT (function), base_ptr);
  5878.         if (base_ptr == error_mark_node)
  5879.           return error_mark_node;
  5880.         return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function));
  5881.           }
  5882.         return function;
  5883.       }
  5884.  
  5885.     assert (TREE_CODE (field) == FIELD_DECL);
  5886.     if (TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE
  5887.         || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE)
  5888.       abort ();
  5889.  
  5890.     TREE_TYPE (rhs) = lhstype;
  5891.     /* First look for an exact match  */
  5892.  
  5893.     while (field && TREE_TYPE (field) != lhstype)
  5894.       field = TREE_CHAIN (field);
  5895.     if (field)
  5896.       {
  5897.         TREE_OPERAND (rhs, 1) = field;
  5898.         return rhs;
  5899.       }
  5900.  
  5901.     /* No exact match found, look for a compatible function.  */
  5902.     field = TREE_OPERAND (rhs, 1);
  5903.     while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
  5904.       field = TREE_CHAIN (field);
  5905.     if (field)
  5906.       {
  5907.         TREE_OPERAND (rhs, 1) = field;
  5908.         field = TREE_CHAIN (field);
  5909.         while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
  5910.           field = TREE_CHAIN (field);
  5911.         if (field)
  5912.           {
  5913.         if (complain)
  5914.           error ("ambiguous overload for COMPONENT_REF requested");
  5915.         return error_mark_node;
  5916.           }
  5917.       }
  5918.     else
  5919.       {
  5920.         if (complain)
  5921.           error ("no appropriate overload exists for COMPONENT_REF");
  5922.         return error_mark_node;
  5923.       }
  5924.     return rhs;
  5925.       }
  5926.  
  5927.     case TREE_LIST:
  5928.       {
  5929.     tree elem, baselink, name;
  5930.     int globals = overloaded_globals_p (rhs);
  5931.  
  5932.     /* If there's only one function we know about, return that.  */
  5933.     if (globals > 0 && TREE_CHAIN (rhs) == NULL_TREE)
  5934.       return TREE_VALUE (rhs);
  5935.  
  5936.     /* First look for an exact match.  Search either overloaded
  5937.        functions or member functions.  May have to undo what
  5938.        `default_conversion' or `datatype' might do to lhstype.  */
  5939.  
  5940.     if (TREE_CODE (lhstype) == POINTER_TYPE)
  5941.       if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
  5942.           || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
  5943.         lhstype = TREE_TYPE (lhstype);
  5944.       else
  5945.         {
  5946.           if (complain)
  5947.         error ("invalid type combination for overload");
  5948.           return error_mark_node;
  5949.         }
  5950.  
  5951.     if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0)
  5952.       {
  5953.         if (complain)
  5954.           error ("cannot resolve overloaded function `%s' based on non-function type",
  5955.              IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
  5956.         return error_mark_node;
  5957.       }
  5958.  
  5959.     if (globals > 0)
  5960.       {
  5961.         assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL);
  5962.         elem = rhs;
  5963.         while (elem)
  5964.           if (TREE_TYPE (TREE_VALUE (elem)) != lhstype)
  5965.         elem = TREE_CHAIN (elem);
  5966.           else
  5967.         return TREE_VALUE (elem);
  5968.         /* No exact match found, look for a compatible function.  */
  5969.         elem = rhs;
  5970.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 1))
  5971.           elem = TREE_CHAIN (elem);
  5972.         if (elem)
  5973.           {
  5974.         tree save_elem = TREE_VALUE (elem);
  5975.         elem = TREE_CHAIN (elem);
  5976.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 0))
  5977.           elem = TREE_CHAIN (elem);
  5978.         if (elem)
  5979.           {
  5980.             if (complain)
  5981.               error ("ambiguous overload for overloaded function requested");
  5982.             return error_mark_node;
  5983.           }
  5984.         return save_elem;
  5985.           }
  5986.         if (complain)
  5987.           {
  5988.         if (TREE_CHAIN (rhs))
  5989.           error ("no appropriate overload for overloaded function `%s' exists",
  5990.              IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
  5991.         else
  5992.           error ("function `%s' has inappropriate type signature",
  5993.              IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
  5994.           }
  5995.         return error_mark_node;
  5996.       }
  5997.  
  5998.     if (TREE_NONLOCAL (rhs))
  5999.       {
  6000.         /* Got to get it as a baselink.  */
  6001.         rhs = lookup_fnfields (CLASSTYPE_AS_LIST (current_class_type),
  6002.                    TREE_PURPOSE (rhs), 0);
  6003.       }
  6004.     else
  6005.       {
  6006.         assert (TREE_CHAIN (rhs) == NULL_TREE);
  6007.         if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST)
  6008.           rhs = TREE_VALUE (rhs);
  6009.         assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL);
  6010.       }
  6011.  
  6012.     for (baselink = rhs; baselink;
  6013.          baselink = next_baselink (baselink))
  6014.       {
  6015.         elem = TREE_VALUE (baselink);
  6016.         while (elem)
  6017.           if (TREE_TYPE (elem) != lhstype)
  6018.         elem = TREE_CHAIN (elem);
  6019.           else
  6020.         return elem;
  6021.       }
  6022.  
  6023.     /* No exact match found, look for a compatible method.  */
  6024.     for (baselink = rhs; baselink;
  6025.          baselink = next_baselink (baselink))
  6026.       {
  6027.         elem = TREE_VALUE (baselink);
  6028.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 1))
  6029.           elem = TREE_CHAIN (elem);
  6030.         if (elem)
  6031.           {
  6032.         tree save_elem = elem;
  6033.         elem = TREE_CHAIN (elem);
  6034.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 0))
  6035.           elem = TREE_CHAIN (elem);
  6036.         if (elem)
  6037.           {
  6038.             if (complain)
  6039.               error ("ambiguous overload for overloaded method requested");
  6040.             return error_mark_node;
  6041.           }
  6042.         return save_elem;
  6043.           }
  6044.         name = DECL_ORIGINAL_NAME (TREE_VALUE (rhs));
  6045.         if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0)
  6046.           {
  6047.         /* Try to instantiate from non-member functions.  */
  6048.         rhs = IDENTIFIER_GLOBAL_VALUE (name);
  6049.         if (rhs && TREE_CODE (rhs) == TREE_LIST)
  6050.           {
  6051.             /* This code seems to be missing a `return'.  */
  6052.             abort ();
  6053.             instantiate_type (lhstype, rhs, complain);
  6054.           }
  6055.           }
  6056.  
  6057.         if (complain)
  6058.           error ("no static member functions named `%s'",
  6059.              IDENTIFIER_POINTER (name));
  6060.         return error_mark_node;
  6061.       }
  6062.       }
  6063.  
  6064.     case CALL_EXPR:
  6065.       /* This is too hard for now.  */
  6066.       assert (0);
  6067.       return error_mark_node;
  6068.  
  6069.     case PLUS_EXPR:
  6070.     case MINUS_EXPR:
  6071.     case COMPOUND_EXPR:
  6072.       TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  6073.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  6074.     return error_mark_node;
  6075.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  6076.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  6077.     return error_mark_node;
  6078.  
  6079.       TREE_TYPE (rhs) = lhstype;
  6080.       return rhs;
  6081.  
  6082.     case MULT_EXPR:
  6083.     case TRUNC_DIV_EXPR:
  6084.     case FLOOR_DIV_EXPR:
  6085.     case CEIL_DIV_EXPR:
  6086.     case ROUND_DIV_EXPR:
  6087.     case RDIV_EXPR:
  6088.     case TRUNC_MOD_EXPR:
  6089.     case FLOOR_MOD_EXPR:
  6090.     case CEIL_MOD_EXPR:
  6091.     case ROUND_MOD_EXPR:
  6092.     case FIX_ROUND_EXPR:
  6093.     case FIX_FLOOR_EXPR:
  6094.     case FIX_CEIL_EXPR:
  6095.     case FIX_TRUNC_EXPR:
  6096.     case FLOAT_EXPR:
  6097.     case NEGATE_EXPR:
  6098.     case ABS_EXPR:
  6099.     case MAX_EXPR:
  6100.     case MIN_EXPR:
  6101.     case FFS_EXPR:
  6102.  
  6103.     case BIT_AND_EXPR:
  6104.     case BIT_IOR_EXPR:
  6105.     case BIT_XOR_EXPR:
  6106.     case LSHIFT_EXPR:
  6107.     case RSHIFT_EXPR:
  6108.     case LROTATE_EXPR:
  6109.     case RROTATE_EXPR:
  6110.  
  6111.     case PREINCREMENT_EXPR:
  6112.     case PREDECREMENT_EXPR:
  6113.     case POSTINCREMENT_EXPR:
  6114.     case POSTDECREMENT_EXPR:
  6115.       if (complain)
  6116.     error ("illegal operation on uninstantiated type");
  6117.       return error_mark_node;
  6118.  
  6119.     case TRUTH_AND_EXPR:
  6120.     case TRUTH_OR_EXPR:
  6121.     case LT_EXPR:
  6122.     case LE_EXPR:
  6123.     case GT_EXPR:
  6124.     case GE_EXPR:
  6125.     case EQ_EXPR:
  6126.     case NE_EXPR:
  6127.     case TRUTH_ANDIF_EXPR:
  6128.     case TRUTH_ORIF_EXPR:
  6129.     case TRUTH_NOT_EXPR:
  6130.       if (complain)
  6131.     error ("not enough type information");
  6132.       return error_mark_node;
  6133.  
  6134.     case COND_EXPR:
  6135.       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
  6136.     {
  6137.       if (complain)
  6138.         error ("not enough type information");
  6139.       return error_mark_node;
  6140.     }
  6141.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  6142.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  6143.     return error_mark_node;
  6144.       TREE_OPERAND (rhs, 2) = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
  6145.       if (TREE_OPERAND (rhs, 2) == error_mark_node)
  6146.     return error_mark_node;
  6147.  
  6148.       TREE_TYPE (rhs) = lhstype;
  6149.       return rhs;
  6150.  
  6151.     case MODIFY_EXPR:
  6152.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  6153.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  6154.     return error_mark_node;
  6155.  
  6156.       TREE_TYPE (rhs) = lhstype;
  6157.       return rhs;
  6158.       
  6159.     case ADDR_EXPR:
  6160.       if (TREE_CODE (lhstype) != POINTER_TYPE)
  6161.     {
  6162.       if (complain)
  6163.         error ("type for resolving address of overloaded function must be pointer type");
  6164.       return error_mark_node;
  6165.     }
  6166.       TREE_TYPE (rhs) = lhstype;
  6167.       lhstype = TREE_TYPE (lhstype);
  6168.       TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  6169.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  6170.     return error_mark_node;
  6171.  
  6172.       mark_addressable (TREE_OPERAND (rhs, 0));
  6173.       return rhs;
  6174.  
  6175.     case ENTRY_VALUE_EXPR:
  6176.       assert (0);
  6177.       return error_mark_node;
  6178.  
  6179.     case ERROR_MARK:
  6180.       return error_mark_node;
  6181.  
  6182.     default:
  6183.       assert (0);
  6184.       return error_mark_node;
  6185.     }
  6186. }
  6187.  
  6188. /* This routine is called when we finally know the type of expression
  6189.    we are looking for.  If the operator encoded by EXP can take an
  6190.    argument of type TYPE, return the FUNCTION_DECL for that operator.  */
  6191. tree
  6192. build_instantiated_decl (type, exp)
  6193.      tree type, exp;
  6194. {
  6195.   tree parmtypes, decl, name;
  6196.  
  6197.   assert (TREE_CODE (exp) == OP_IDENTIFIER);
  6198.   type = datatype (type);
  6199.   if (TREE_CODE (type) != POINTER_TYPE
  6200.       || (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
  6201.       && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE))
  6202.     {
  6203.       error ("invalid type used to resolve overloaded function");
  6204.       return error_mark_node;
  6205.     }
  6206.  
  6207.  
  6208.   /* Now we know the type of this function, so overload it.  */
  6209.   parmtypes = TYPE_ARG_TYPES (TREE_TYPE (type));
  6210.   name = build_operator_fnname (&exp, parmtypes, 0);
  6211.   if (name)
  6212.     {
  6213.       name = build_decl_overload (IDENTIFIER_POINTER (name), parmtypes, 1);
  6214.       decl = lookup_name (name);
  6215.       if (decl)
  6216.     return decl;
  6217.       error ("no suitable declaration of `operator %s' for overloading",
  6218.          operator_name_string (name));
  6219.       return error_mark_node;
  6220.     }
  6221.   return error_mark_node;
  6222. }
  6223.  
  6224. /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
  6225.    for the given TYPE.  */
  6226. static tree
  6227. get_vtable_name (type)
  6228.      tree type;
  6229. {
  6230.   char *buf = (char *)alloca (sizeof (VTABLE_NAME_FORMAT)
  6231.                   + TYPE_NAME_LENGTH (type) + 2);
  6232.   sprintf (buf, VTABLE_NAME_FORMAT, TYPE_NAME_STRING (type));
  6233.   return get_identifier (buf);
  6234. }
  6235.  
  6236. /* Return the name of the virtual function pointer field
  6237.    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
  6238.    this may have to look back through base types to find the
  6239.    ultimate field name.  (For single inheritance, these could
  6240.    all be the same name.  Who knows for multiple inheritance).  */
  6241. static tree
  6242. get_vfield_name (type)
  6243.      tree type;
  6244. {
  6245.   char *buf;
  6246.  
  6247.   while (CLASSTYPE_N_BASECLASSES (type)
  6248.      && TYPE_VIRTUAL_P (CLASSTYPE_BASECLASS (type, 1))
  6249.      && ! CLASSTYPE_VIA_VIRTUAL (type, 1))
  6250.     type = CLASSTYPE_BASECLASS (type, 1);
  6251.  
  6252.   buf = (char *)alloca (sizeof (VFIELD_NAME_FORMAT)
  6253.             + TYPE_NAME_LENGTH (type) + 2);
  6254.   sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
  6255.   return get_identifier (buf);
  6256. }
  6257.  
  6258. void
  6259. print_class_statistics ()
  6260. {
  6261. #ifdef GATHER_STATISTICS
  6262.   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
  6263.   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
  6264.   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
  6265.        n_build_method_call, n_inner_fields_searched);
  6266.   if (n_vtables)
  6267.     {
  6268.       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
  6269.            n_vtables, n_vtable_searches);
  6270.       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
  6271.            n_vtable_entries, n_vtable_elems);
  6272.     }
  6273. #endif
  6274. }
  6275.